title: How to modify a static private field during tests?
body: <p>My project uses <code>JUnit</code>, <code>Mockito</code>, <code>PowerMockito</code> to create a unit test. The code as below:</p>
  
  <pre><code>public class FirstController {
      public void doSomething() {
          ServiceExecutor.execute();
      }
  }
  
  public class ServiceExecutor {
  
      private static final List&lt;Service&gt; services = Arrays.asList(
          new Service1(),
          new Service2(),
          ...
      );
  
      public static void execute() {
          for (Service s : services) {
              s.execute();
          }
      }   
  }
  
  @RunWith(PowerMockRunner.class)
  @PrepareForTest({ServiceExecutor.class})
  public class FirstControllerTest {
  
          @Before
          public void prepareForTest() {
              PowerMockito.mockStatic(ServiceExecutor.class);
              PowerMockito.doNothing().when(ServiceExecutor.class)
          }
  
          @Test
          public void doSomethingTest() {
              FirstController firstController = new FirstController();
              firstController.doSomething();
              PowerMockito.verifyStatic(ServiceExecutor.class, Mockito.times(1));
          }   
      }
  </code></pre>
  
  <p>The full source code of this issue: <a href="https://github.com/gpcodervn/Java-Tutorial/tree/master/UnitTest" rel="nofollow noreferrer">https://github.com/gpcodervn/Java-Tutorial/tree/master/UnitTest</a></p>
  
  <p>I want to verify the <code>ServiceExecutor.execute()</code> method which was run. </p>
  
  <p>I tried to mock <code>ServiceExecutor</code> and <code>doNothing()</code> when the <code>execute()</code> method is called. But I have a problem with the <code>private static final List&lt;Service&gt; services</code> in the <code>ServiceExecutor</code>. It always constructs the new instance for each Service. Each service is longer to create the new instance and I don't know how many Service they will have later if I mock each <code>Service</code>.</p>
  
  <p>Do you have any idea to verify <code>ServiceExecutor.execute()</code> in <code>FirstController</code> without run any method in <code>ServiceExecutor</code>?</p>