Java PowerMockito Mocking Instant.now()

时间:2016-06-02 15:39:49

标签: java unit-testing mockito powermock

我试图模拟静态方法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);
    }

}
  

错误1   org.mockito.exceptions.misusing.UnfinishedStubbingException:       这里检测到未完成的存根:        - >在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
     

错误2:原因:[源错误] toInstant()未找到   java.time.ZonedDateTime

2 个答案:

答案 0 :(得分:0)

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

(这与您的错误消息不完全匹配,但仍然是一个非常清楚的原因,为什么你在这里遇到麻烦,你不会在Mockito和PowerMock中使用非系统类。)< / p>

模拟系统类的危险 - 除了你正在嘲笑数据对象,特别是具有明确的单元测试功能的对象(如评论中提到的时钟覆盖Andy Turner) - 是一个很好的理由< em>不在这里使用模拟。

答案 1 :(得分:0)

从我能够告诉我得到错误,因为我最初在我的单元测试中调用.now()然后尝试模拟它。这是因为我认为我可以让我的测试使用unmocked方法,然后模拟它,这样我的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()使用我的Test类,而我的SUT源使用.ofInstant(...)