【发布时间】:2018-09-04 08:57:13
【问题描述】:
考虑问题https://stackoverflow.com/a/51980599/7203487。 类中的方法中只有一个方法包含需要模拟的 System.getenv。问题是我需要采用 jacoco 代码覆盖率,由于使用 powemock,我得到的覆盖率为 0%。有没有办法在有或没有 powermock 的情况下模拟系统并获得代码覆盖率?
【问题讨论】:
标签: java junit powermockito
考虑问题https://stackoverflow.com/a/51980599/7203487。 类中的方法中只有一个方法包含需要模拟的 System.getenv。问题是我需要采用 jacoco 代码覆盖率,由于使用 powemock,我得到的覆盖率为 0%。有没有办法在有或没有 powermock 的情况下模拟系统并获得代码覆盖率?
【问题讨论】:
标签: java junit powermockito
查看 JUnit 4 的 System Rules,尤其是 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>
【讨论】: