[英]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.