简体   繁体   English

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

[英]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.

相关问题 Powermock(使用Easymock)没有最后一个可用的模拟电话 - Powermock (With Easymock) no last call on a mock available java.lang.IllegalStateException:对PowerMock和EasyMock可用的模拟没有最后一次调用 - java.lang.IllegalStateException: no last call on a mock available with PowerMock and EasyMock 使用PowerMock和Mockito模拟静态方法 - Mocking static methods with PowerMock and Mockito PowerMock :: [java.lang.IllegalStateException:没有最后一次调用可用的模拟] - PowerMock:: [java.lang.IllegalStateException: no last call on a mock available] PowerMock,模拟一个静态方法,然后在所有其他静态上调用真正的方法 - PowerMock, mock a static method, THEN call real methods on all other statics 使用Powermock的@Mock中的Nullpointer(模拟最终的静态Hashtable) - Nullpointer in a @Mock (mocking final static Hashtable) with Powermock PowerMock-模拟静态系统类会抛出IllegalStateException - PowerMock - Mocking static system class throws IllegalStateException 如何使用Easymock / Powermock模拟对象响应带参数的方法 - How to use easymock/powermock mock objects to respond to methods with arguments 如何使用JUnit,EasyMock或PowerMock模拟静态最终变量 - How to mock a static final variable using JUnit, EasyMock or PowerMock PowerMock和EasyMock的方法嘲讽问题 - PowerMock and EasyMock method mocking issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM