EasyMock / PowerMock-模拟静态方法将引发错误:没有对模拟的最后一次调用

时间:2019-06-24 10:32:40

标签: java gradle static powermock easymock

我正在编写一个简单的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上浏览了很多帖子之后;我终于想到要在这里问这个问题。

希望这里的专家会帮助我。预先感谢!

1 个答案:

答案 0 :(得分:1)

您似乎正在使用TestNG(请参阅导入内容)。但是使用的运行程序是JUnit运行程序。

然后,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());
    }
}