Java PowerMockito 模拟 Instant.now()

Java PowerMockito Mocking Instant.now()

我正在尝试模拟静态方法 Instant.now() 并且在尝试从 java.time 包中模拟 类 时我继续遇到奇怪的行为。请参阅下面关于尝试模拟 Instant.now()

的代码
@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);
    }

}

Errors 1 org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:495)

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

Errors 2: with Reason: [source error] toInstant() not found in java.time.ZonedDateTime

您正在尝试模拟一个 java.time class,这意味着您正在尝试模拟一个系统 class。这需要你 put your own system under test in the @PrepareForTest annotation,因为 PowerMock 无法拦截你正在调用的 class 的加载,所以它必须拦截你的 class 的加载并执行它的字节码 -在那里重写。

(这与您的错误消息不完全匹配,但仍然是您在这里遇到问题的一个非常明确的原因,而非系统 classes 在Mockito 和 PowerMock 两者。)

模拟系统的危险 classes——除了模拟数据对象这一事实之外,尤其是具有明确单元测试功能的对象(如评论中提到的时钟覆盖 Andy Turner )—在这里使用模拟的一个很好的理由。

据我所知,我得到了错误,因为最初我在我的单元测试中调用 .now() 然后试图模拟它。这样做的原因是因为我认为我可以让我的测试使用未模拟的方法然后模拟它,这样我的 SUT 就可以使用模拟的形式。

我最后稍微改变了一下,取而代之的是嘲笑 ZonedDateTime.ofInstant(obj,obj)。这就是我所做的

@Test
    public void renamingToArbitraryMethodName() throws Exception {
        ZonedDateTime current = ZonedDateTime.now();
        ZonedDateTime mockMidNight = ZonedDateTime.of(current.getYear(), current.getMonthValue(),
                                                      current.getDayOfMonth(), 0, 0, 0, 0, ZoneId.of("US/Eastern"));

        PowerMockito.mockStatic(ZonedDateTime.class);
        PowerMockito.when(ZonedDateTime.ofInstant(anyObject(), anyObject())).thenReturn(mockTime);
    }

在我的场景中,这对我有用,因为我能够使用 .now() 完成我的测试 class,而我的 SUT 源使用 .ofInstant(...)