有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用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) 个答案

  1. # 1 楼答案

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