Potential bug with static mocked `Instant.now()` working with `DateTimeFormatter`

190 views
Skip to first unread message

Thomas Wang

unread,
Dec 12, 2021, 1:21:44 PM12/12/21
to mockito
Within the `try` block for the mocked `Instant`, calling `format` failed with `NullPointerException`. Honestly, I'm not quite sure if this is a `mockito` or `DateTimeFormatter` or `Instant` issue. Could someone help me take a look? Thanks.

Here is my test:
```
    @Test
    public void testAdd() {
        Instant mockedNow = Instant.now(Clock.fixed(Instant.parse("2021-12-01T10:00:00Z"), ZoneOffset.UTC));
        DateTimeFormatter nowSimpleDateFormat = DateTimeFormatter
                .ofPattern("yyyyMMdd'_'HHmmss")
                .withLocale(Locale.US)
                .withZone(ZoneId.systemDefault());

        System.out.println(nowSimpleDateFormat.format(Instant.now()));
        // this line passes ok
        assertEquals(nowSimpleDateFormat.format(mockedNow), "20211201_020000");

        // but once I add the stubbing, the same assertion failed with a NullPointerException
        try (MockedStatic<Instant> mockedInstant = mockStatic(Instant.class)) {
            mockedInstant.when(Instant::now).thenReturn(mockedNow);
            // same assertion as line 27
            assertEquals(nowSimpleDateFormat.format(mockedNow), "20211201_020000");
        }
    }
```

Here is the stacktrace.
```
java.lang.NullPointerException: instant

        at java.base/java.util.Objects.requireNonNull(Objects.java:246)
        at java.base/java.time.ZonedDateTime.ofInstant(ZonedDateTime.java:407)
        at java.base/java.time.chrono.IsoChronology.zonedDateTime(IsoChronology.java:399)
        at java.base/java.time.chrono.IsoChronology.zonedDateTime(IsoChronology.java:126)
        at java.base/java.time.format.DateTimePrintContext.adjust(DateTimePrintContext.java:150)
        at java.base/java.time.format.DateTimePrintContext.<init>(DateTimePrintContext.java:119)
        at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1841)
        at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1817)
        at io.datability.HelloTest.testAdd(HelloTest.java:31)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
        at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
        at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
        at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
```

I'm using Java 11. Here is my POM dependencies:
```
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.8.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.8.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-inline</artifactId>
      <version>4.1.0</version>
      <scope>test</scope>
    </dependency>
```
Reply all
Reply to author
Forward
0 new messages