title: Java PowerMockito Mocking Instant.now()
body: <p>I'm attempting to Mock the static method <code>Instant.now()</code> and I continue to keep coming across weird behavior when trying to mock classes from <code>java.time</code> package. Please see my code below on attempting to mock <code>Instant.now()</code></p>
  
  <pre><code>@RunWith(PowerMockRunner.class)
  @PrepareForTest(Instant.class)
  public class UnitTestClasss {
      @Test
      public void unitTestMethod() throws Exception {
          mockCurrentTimeAtMidNight();
          instanceOfSystemUnderTest.someMethodDependingOnTime();
          assertHandledHere();
      }
  
      /*See First Error Below */
      private void mockCurrentTimeAtMidNight() {
          ZonedDateTime current = ZonedDateTime.now();
          ZonedDateTime mockMidNight = ZonedDateTime.of(current.getYear(), current.getMonthValue(),
                  current.getDayOfMonth(), 0, 0, 0, 0,current.getZone());
  
          PowerMockito.mockStatic(Instant.class);
          PowerMockito.when(Instant.now()).thenReturn(Instant.from(mockMidNight));
      }
  
      /*See Second Error Below */
      private void mockCurrentTimeAtMidNight2() {
          Calendar cal = Calendar.getInstance();
  
          ZonedDateTime mockMidNight = ZonedDateTime.of(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                  cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0, 0,ZoneId.of("US/Eastern"));
          Instant instant = mockMidNight.toInstant();
          PowerMockito.mockStatic(Instant.class);
          PowerMockito.when(Instant.now()).thenReturn(instant);
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>Errors 1
    org.mockito.exceptions.misusing.UnfinishedStubbingException: 
        Unfinished stubbing detected here:
        -> at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:495)</p>
  
  <pre><code>E.g. thenReturn() may be missing.
  Examples of correct stubbing:
      when(mock.isOk()).thenReturn(true);
      when(mock.isOk()).thenThrow(exception);
      doThrow(exception).when(mock).someVoidMethod();
  Hints:
   1. missing thenReturn()
   2. you are trying to stub a final method, you naughty developer!
   3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
  </code></pre>
    
    <p>Errors 2: with Reason: [source error] <code>toInstant()</code> not found in
    <code>java.time.ZonedDateTime</code></p>
  </blockquote>