如何使用JMock验证两个方法调用之间已经过了一定时间?

时间:2015-05-02 22:14:28

标签: java junit jmock

我正在使用JMock和JUnit来测试程序。我想验证两次方法调用之间经过了一段时间,我怎么能这样做?我试图通过javadocs查看JMock,我没有运气。也许没有办法?

或者,如果这可以通过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();
    }});

1 个答案:

答案 0 :(得分:1)

我总是使用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();
}});