Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking System.getenv other than with powermock

Considering question https://stackoverflow.com/a/51980599/7203487. Only one method among methods in class contains System.getenv which needs to be mocked. Problem is I require to take jacoco code coverage which I get as 0% due to use of powemock. Is there a way possible to mock system and attain code coverage with or without powermock?

like image 871
Gayathri Avatar asked Dec 23 '22 04:12

Gayathri


1 Answers

Have a look into the System Rules for JUnit 4, especially the EnvironmentVariables.

public class EnvironmentVariablesTest {

  @Rule
  public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

  @Test
  public void setEnvironmentVariable() {
    environmentVariables.set("name", "value");
    assertEquals("value", System.getenv("name"));
  }
}
like image 176
Roland Weisleder Avatar answered Jan 09 '23 14:01

Roland Weisleder