Hi Johan,
I did some testing with TestNG 5.14.2, EasyMock 3 and PowerMock 1.4.6 around this. I addressed the point you raised below by delegating the system call into a method in the class under test. I then partially mocked the class under test, specifying only the method that performing the system call.
The outcome was the same as before:
no last call on a mock availableThe class under test, the test and stack trace are below. It looks like an issue with PM
expectsPrivate intercepting the expects call to EasyMock...
I would appreciate any help!
TIA
Martin
---------
The class under test:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IAX {
private static InetAddress IA;
public InetAddress getIA() {
if (IA == null) {
IA = getLocalHost();
}
return IA;
}
// public static InetAddress getSIA() {
// return getLocalHost();
// }
private final InetAddress getLocalHost() {
try {
return InetAddress.getLocalHost();
}
catch (final UnknownHostException e) {
return null;
}
}
}
---------
The test
import static org.testng.Assert.assertNotNull;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@PrepareForTest(IAX.class)
public class IAXTest {
InetAddress addr;
IAX classUnderTest;
@BeforeClass
public void setUp() throws UnknownHostException {
addr = InetAddress.getLocalHost();
classUnderTest = PowerMock.createPartialMock(IAX.class, "getLocalHost");
}
/**
* Test method for {@link IAX#getIA()}.
*
* @throws Exception
*/
@Test()
public void testGetIA() throws Exception {
PowerMock.reset(classUnderTest);
/*
PowerMock.expectPrivate(classUnderTest, "getLocalHost").andThrow(
new UnknownHostException());
*/
PowerMock.expectPrivate(classUnderTest, "getLocalHost").andReturn(addr);
PowerMock.replay(classUnderTest);
assertNotNull(classUnderTest.getIA());
}
/**
* Test method for {@link IAX#getSIA()}.
*
* @throws Exception
*/
//@Test()
public void testGetSIA() throws Exception {
PowerMock.reset(classUnderTest);
/*
PowerMock.expectPrivate(classUnderTest, "getLocalHost").andThrow(
new UnknownHostException());
*/
PowerMock.expectPrivate(classUnderTest, "getLocalHost").andReturn(
InetAddress.getLocalHost());
PowerMock.replay(classUnderTest);
assertNotNull(IA.getSIA());
}
}
--------
The stack trace
FAILED: testGetIA
java.lang.IllegalStateException: no last call on a mock available
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:521)
at org.easymock.EasyMock.expectLastCall(EasyMock.java:512)
at org.powermock.api.easymock.PowerMock.doExpectPrivate(PowerMock.java:2247)
at org.powermock.api.easymock.PowerMock.expectPrivate(PowerMock.java:1409)
at org.powermock.api.easymock.PowerMock.expectPrivate(PowerMock.java:1420)
at org.powermock.api.easymock.PowerMock.expectPrivate(PowerMock.java:1366)
at IAXTest.testGetIA(IAXTest.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:675)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:848)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1170)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1119)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:600)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:315)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:310)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:272)
at org.testng.SuiteRunner.run(SuiteRunner.java:221)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:945)
at org.testng.TestNG.runSuitesLocally(TestNG.java:884)
at org.testng.TestNG.run(TestNG.java:818)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:96)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:179)