[英]Mocking System.getenv other than with powermock
考虑问题https://stackoverflow.com/a/51980599/7203487 类中的方法中只有一种方法包含System.getenv,需要对其进行模拟。 问题是我需要使用jacoco代码覆盖率,由于使用powemock,我得到的覆盖率为0%。 有没有办法在有或没有powermock的情况下模拟系统并获得代码覆盖率?
看一下JUnit 4的系统规则 ,尤其是EnvironmentVariables 。
public class EnvironmentVariablesTest {
@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
@Test
public void setEnvironmentVariable() {
environmentVariables.set("name", "value");
assertEquals("value", System.getenv("name"));
}
}
添加到@Roland Weisleder。
EnvironmnetVariables不是junit的一部分。 您需要添加以下依赖项
https://stefanbirkner.github.io/system-rules/download.html
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.