title: how to use System.getenv in Junit PowerMockito
body: <p>I'm able to mock the System.getenv value from Junit but when I execute the test case - in my service class System.getevn value coming as null. Not sure what I'm doing wrong here. Please find my test service class and junit class.</p>
  
  <p>Can some please help me to fix this issue - why the value is not setting in my actual service class?</p>
  
  <p><strong>TestService.java</strong></p>
  
  <pre><code>public class TestService {
  
      public TestService() throws Exception {
  
          loadTestMethod();
  
      }
  
      private void loadTestMethod() {
  
          System.out.println("Environment vairlable : " + System.getenv("my_key_name"));
          System.setProperty("app.key", System.getenv("my_key_name"));
  
      }
  
  }
  </code></pre>
  
  <p><strong>TestServiceTest.java</strong></p>
  
  <pre><code>import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.Mock;
  import org.mockito.MockitoAnnotations;
  import org.powermock.api.mockito.PowerMockito;
  import org.powermock.core.classloader.annotations.PowerMockIgnore;
  import org.powermock.core.classloader.annotations.PrepareForTest;
  import org.powermock.modules.junit4.PowerMockRunner;
  
  @RunWith(PowerMockRunner.class)
  @PrepareForTest(value = { System.class })
  @PowerMockIgnore("javax.management.*")
  
  public class TestServiceTest {
  
      @Mock
      TestService testService;
  
      @Before
      public void setUp() throws Exception {
          MockitoAnnotations.initMocks(this);
  
          PowerMockito.mockStatic(System.class);
          PowerMockito.when(System.getenv("my_key_name")).thenReturn("Testing");
          System.out.println("Junit Environment vairlable : " + System.getenv("my_key_name"));
          testService = new TestService();
  
      }
  
      @Test
      public void myServiceTest() {
  
      }
  }
  </code></pre>