我现在面临的问题,让每方法老虎钳System.getProperty("name")
值嘲讽System.class
实例后:模拟System.class在JUnit
@Test
public void Test1()throws Exception {
PowerMockito.mockStatic(System.class);
// Configuration
when(System.getProperty("os.name")).thenReturn("Win");
}
@Test
public void Test2() throws Exception {
PowerMockito.mockStatic(System.class);
when(System.getProperty("os.name")).thenReturn("Linux");
}
我们有两个测试方法,当我们运行上面提到的两个方法独立,那么我们得到正确的值System.getProperty("os.name")
。但是当我们正在运行类(将在类中执行这两种方法),那么我们在第二种方法中获得第一个方法的System.getProperty("os.name")
值。 请建议。
提前致谢。
你可以添加整个班级代码吗? – developer
我无法复制,请提供整个测试类 –
如果您想测试系统属性,您应该将'System'套装在桥中。想想像[系统规则](http://stefanbirkner.github.io/system-rules/)。 –