gpt4 book ai didi

java - 如何使用JMock来验证两个方法调用之间是否经过了一定的时间?

转载 作者:行者123 更新时间:2023-12-01 11:35:49 24 4
gpt4 key购买 nike

我正在使用 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();
}});

关于java - 如何使用JMock来验证两个方法调用之间是否经过了一定的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30008072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com