我正在使用JMock和JUnit来测试程序。我想验证两次方法调用之间是否经过了一定的时间,我该怎么做?我正在尝试浏览JMock的javadocs,但没有运气。也许没有办法?

或者,如果可以使用JUnit做到这一点,那么效果也很好。

   context.checking(new Expectations() {{
        // Set up action that causes the change of state
        oneOf(lightTimerInterfaceMock).startTimer(5);
        oneOf(lightDeviceInterfaceMock).turnLightOnFullBrightness();'
        // I want to verify 5 seconds has elapsed here
        oneOf(lightDeviceInterfaceMock).turnLightOff();
    }});

最佳答案

我总是使用System.nanoTime():

context.checking(new Expectations() {{
    // Set up action that causes the change of state
    // oneOf(lightTimerInterfaceMock).startTimer(5);
    long startTime = System.nanoTime();
    oneOf(lightDeviceInterfaceMock).turnLightOnFullBrightness();'
    // I want to verify 5 seconds has elapsed here
    long endTime = System.nanoTime();
    boolean fiveSecondsPassed = (endTime - startTime) / 1000000000 >= 5
    oneOf(lightDeviceInterfaceMock).turnLightOff();
}});

07-25 20:29