我想为我的项目中的一些静态方法编写单元测试用例,
我的类(class)代码片段,
Class Util{
public static String getVariableValue(String name)
{
if(isWindows()){
return some string...
}
else{
return some other string...
}
}
public static boolean isWindows(){
if(os is windows)
return true;
else
return false;
}
}
基本上,我想在 isWindows() 返回“false”时为 getVariableValue() 编写单元测试用例。我如何使用 powermock 编写这个?
最佳答案
该解决方案还使用 Easymock 来设置期望。 首先,您需要准备测试类:
@RunWith(PowerMockRunner.class)
@PrepareForTest(Util.class)
public class UtilTest {}
模拟静态类:
PowerMock.mockStaticPartial(Util.class,"isWindows");
设定一个期望:
EasyMock.expect(Util.isWindows()).andReturn(false);
重播模拟:
PowerMock.replay(Util.class);
调用您想要测试的方法,然后使用以下方法验证模拟:
PowerMock.verify(Util.class);
关于java - 使用 powermock 对静态方法进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18613193/