[英]EasyMock/PowerMock - Mocking Static Methods Throws Error: no last call on a mock available
I'm writing a simple Java Class to test EasyMock/PowerMock functionality to mock static methods from class. 我正在编写一个简单的Java类来测试EasyMock / PowerMock功能,以从类中模拟静态方法。
So I'm just mocking Math.random
method to return a constant
value for testing purpose. 因此,我只是在模拟
Math.random
方法以返回constant
值以进行测试。
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
, 我正在使用:
easyMock: org.easymock:easymock:3.1
,
powerMockEasyMockFull: org.powermock:powermock-easymock-release-full:1.5.1
with java 1.7.0_80
. 使用
java 1.7.0_80
。
But Everytime I try to run this test class using testng
; 但是每当我尝试使用
testng
运行这个测试类的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; 在SO和Google上浏览了很多帖子之后; I finally thought of asking this question here.
我终于想到在这里问这个问题。
Hope experts here will help me out. 希望这里的专家能帮助我。 Thanks in Advance!
提前致谢!
You seem to use TestNG (seeing the imports). 您似乎正在使用TestNG(请参阅导入内容)。 But the runner used is a JUnit runner.
但是使用的运行程序是JUnit运行程序。
Then, PowerMock doesn't work with BeforeClass
. 然后,PowerMock无法与
BeforeClass
。 You need to use a Before
. 您需要使用
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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.