[英]java.lang.IllegalArgumentException: Not a mock: java.lang.Class on PowerMock and EasyMock
I have a test case using PowerMock to test on static method from Math, as 我有一个使用PowerMock测试来自Math的静态方法的测试用例
@RunWith(PowerMockRunner.class)
@PrepareForTest( { Math.class })
public class Test{
@Test
public void test2(){
PowerMockito.mockStatic(Math.class);
EasyMock.expect(Math.abs(-123)).andReturn(1);
EasyMock.replay(Math.class);
long returns = Math.abs(-123);
EasyMock.verify(Math.class);
org.junit.Assert.assertEquals(1,returns);
}
}
My pom.xml looks as 我的pom.xml看起来像
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
And I got error as 我得到了错误
java.lang.IllegalArgumentException: Not a mock: java.lang.Class
at org.easymock.internal.MocksControl.getControl(MocksControl.java:175)
at org.easymock.EasyMock.getControl(EasyMock.java:2130)
at org.easymock.EasyMock.replay(EasyMock.java:2032)
This is the first time I'm using PowerMock, I just want to try to test static method, and I Choose Java Math class to test. 这是我第一次使用PowerMock,我只想尝试测试静态方法,并选择Java Math类进行测试。 I think I have done all the parts in the instructions from https://code.google.com/p/powermock/wiki/MockStatic 我想我已经完成了https://code.google.com/p/powermock/wiki/MockStatic说明中的所有部分
What's wrong with my test case? 我的测试用例有什么问题?
I face this issue and I resolve it by using PowerMock.replay(StaticClass.class)
我遇到这个问题,我通过使用PowerMock.replay(StaticClass.class)
来解决它
In your case, all you have to do is replace EasyMock.replay(Math.class);
在您的情况下,您所要做的就是替换EasyMock.replay(Math.class);
with PowerMock.replay(Math.class);
与PowerMock.replay(Math.class);
since you are already used "PowerMock". 因为你已经使用过“PowerMock”了。
Instead of using EasyMock, I use mockito to replace it, code as 而不是使用EasyMock,我使用mockito来替换它,代码为
PowerMockito.mockStatic(Math.class);
Mockito.when(Math.abs(-123)).thenReturn(1);
int returns = Math.abs(-123);
This code is working fine for me in Java 7, but in java 8, it fails 在Java 7中,这段代码对我来说很好,但在java 8中,它失败了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.