Reputation: 4682
I'm writing a simple Java Class to test EasyMock/PowerMock functionality to mock static methods from class.
So I'm just mocking Math.random
method to return a constant
value for testing purpose.
Here is my code:
package x.y.z;
import org.easymock.EasyMock;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@RunWith(PowerMockRunner.class)
@PrepareForTest(Math.class)
public class PowerMockStaticTestExample {
@BeforeClass
public static void setupBeforeClass() {
try {
PowerMock.mockStatic(Math.class);
EasyMock.expect(Math.random()).andReturn(0.50).anyTimes();
PowerMock.replay(Math.class);
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Test
public void dummyTest()
{
System.out.println("DummyTest Called!");
assert true==true;
}
@Test
public void testMath()
{
System.out.println("Math Test Start "+Math.random());
assert true==true;
}
}
Dependencies:
I'm using: easyMock: org.easymock:easymock:3.1
,
powerMockEasyMockFull: org.powermock:powermock-easymock-release-full:1.5.1
with java 1.7.0_80
.
But Everytime I try to run this test class using testng
; It throws following Exception:
java.lang.IllegalStateException: no last call on a mock available
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:520)
at org.easymock.EasyMock.expect(EasyMock.java:498)
at x.y.z.PowerMockStaticTestExample.setupBeforeClass(PowerMockStaticTestExample.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
After going through lot of posts on SO and Google; I finally thought of asking this question here.
Hope experts here will help me out. Thanks in Advance!
Upvotes: 0
Views: 888
Reputation: 5721
You seem to use TestNG (seeing the imports). But the runner used is a JUnit runner.
Then, PowerMock doesn't work with BeforeClass
. You need to use a Before
. Here is a working example.
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(Math.class)
public class PowerMockStaticTestExample {
@Before
public void setupBeforeClass() {
PowerMock.mockStatic(Math.class);
EasyMock.expect(Math.random()).andReturn(0.50).anyTimes();
PowerMock.replay(Math.class);
}
@Test
public void dummyTest() {
System.out.println("DummyTest Called!");
}
@Test
public void testMath() {
System.out.println("Math Test Start "+Math.random());
}
}
Upvotes: 1