我正在编写一个简单的Java类,以测试easymock/powerMock功能从类模拟静态方法。
所以我只是嘲笑Math.random
方法以返回constant
值以进行测试。
这是我的代码:
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;
}
}
依赖项:
我正在使用: easyMock: org.easymock:easymock:3.1
,
powerMockEasyMockFull: org.powermock:powermock-easymock-release-full:1.5.1
使用java 1.7.0_80
。
但是,每次我尝试使用testng
运行此测试类;它以外的例外:
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)
经过So和Google的大量帖子后;我终于在这里想到这个问题。
希望这里的专家会帮助我。预先感谢!
您似乎使用testng(查看导入(。但是跑步者所使用的是Junit Runner。
然后,PowerMock与BeforeClass
无法使用。您需要使用Before
。这是一个工作示例。
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());
}
}