[英]PowerMockito mock indirect static method
要测试的课程
public class Randomer {
public int get() {
return (int) Math.random() + 1;
}
}
测试班
package org.samiron;
import static org.junit.Assert.assertEquals;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.support.membermodification.MemberMatcher;
import org.powermock.api.support.membermodification.MemberModifier;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.testng.annotations.Test;
/**
* @author samiron
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Randomer.class, Math.class })
public class RandomerTest {
@Test
public void shouldAddUpDieRollsCorrectly() throws Exception {
PowerMockito.spy(Math.class);
MemberModifier.stub(MemberMatcher.method(Math.class, "random")).toReturn(2.0);
Randomer d = new Randomer();
assertEquals(3, d.get());
}
}
始终获取java.lang.AssertionError: expected:<3> but was:<1>
怎么了 老实说,每次我遇到一个模拟静态函数的情况时,我都会设法找到解决方法,而不是浪费时间。 因此,需要您的帮助以找出确切的解决方案。
示例类的唯一目的是证明Math.random()函数不会被模拟,因此不会返回所需的值。
一般实现
在编写测试时,模拟是每个必不可少的工具。 尽管对实例进行模拟的工作效果与预期的相当,但是对模拟静态方法的模拟似乎真的很复杂,因为模拟库的组合如此之多,并且有很多选项仅支持少数几种简单方案。 这应该简化。
使用的库:
通过此测试,从而证明静态调用Math.random()
已成功模拟:
@RunWith(PowerMockRunner.class)
// tell PowerMock about (a) the class you are going to test and (b) the class you are going to 'mock static'
@PrepareForTest({Randomer.class, Math.class })
public class RandomerTest {
@Test
public void shouldAddUpDieRollsCorrectly() throws Exception {
// prepare PowerMock for mocking statics on Math
PowerMockito.mockStatic(Math.class);
// establish an expectation for what Match.random() should return
PowerMockito.when(Math.random()).thenReturn(2.0);
Randomer d = new Randomer();
assertEquals(3, d.get());
}
}
这与您在问题中发布的内容之间的主要区别是,我正在使用...
PowerMockito.mockStatic(Math.class)
和PowerMockito.when(Math.random()).thenReturn(2.0)
... 代替:
PowerMockito.spy(Math.class)
和PowerMockito.spy(Math.class)
MemberModifier.stub(MemberMatcher.method(Math.class, "random")).toReturn(2.0)
另外,在您的OP中,示例代码使用了JUnit( org.powermock.modules.junit4.PowerMockRunner
)和TestNG( org.testng.annotations.Test
)的混合物,而在我的示例中,我只是使用JUnit。
以上已通过验证
junit:4.12
powermock-module-junit4:1.7.0
powermock-api-mockito2:1.7.0
似乎这里的库不匹配。
在注释中,您声明要使用以下依赖项(Maven不方便使用):
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-mockito-release-full</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
我让您的代码使用以下代码:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.