82

我现在正在编写单元测试。我需要使用 Mockito 模拟长期运行方法来测试我的实现的超时处理。Mockito 可以吗?

像这样的东西:

when(mockedService.doSomething(a, b)).thenReturn(c).after(5000L);
4

4 回答 4

100

您可以简单地让线程休眠所需的时间。当心 - 这样的事情确实会减慢您的自动化测试执行速度,因此您可能希望将此类测试隔离在单独的套件中

它看起来类似于:

when(mock.load("a")).thenAnswer(new Answer<String>() {
   @Override
   public String answer(InvocationOnMock invocation){
     Thread.sleep(5000);
     return "ABCD1234";
   }
});
于 2012-10-10T07:14:07.440 回答
49

从 mockito 2.8.44 开始,org.mockito.internal.stubbing.answers.AnswersWithDelay可用于此目的。这是一个示例用法

 doAnswer( new AnswersWithDelay( 1000,  new Returns("some-return-value")) ).when(myMock).myMockMethod();
于 2018-05-25T13:24:06.587 回答
18

我为此创建了一个实用程序:

import java.time.Duration;
import java.util.concurrent.TimeUnit;

import static org.mockito.Mockito.doAnswer;

public class Stubber {

    public static org.mockito.stubbing.Stubber doSleep(Duration timeUnit) {
        return doAnswer(invocationOnMock -> {
            TimeUnit.MILLISECONDS.sleep(timeUnit.toMillis());
            return null;
        });
    }

    public static <E> org.mockito.stubbing.Stubber doSleep(Duration timeUnit, E ret) {
        return doAnswer(invocationOnMock -> {
            TimeUnit.MILLISECONDS.sleep(timeUnit.toMillis());
            return ret;
        });
    }

}

在您的测试用例中只需使用:

doSleep(Duration.ofSeconds(3)).when(mock).method(anyObject());
于 2017-04-12T09:44:14.397 回答
-1

单元测试更好的是创建调用实际 Thread.sleep(long l) 的方法,然后模拟该方法。有了它,您可以为您的测试注入令人敬畏的行为,从而使您的测试认为它正在等待您想要的时间。有了它,您可以在眨眼之间运行大量测试,并且仍然可以测试不同的时间相关场景。在使用它之前,我的 UnitTest 运行了六分钟。现在它低于 200 毫秒。

public class TimeTools {
public long msSince(long msStart) {
    return ((System.nanoTime() / 1_000_000) - msStart);
}

public long msNow() {
    return (System.nanoTime() / 1_000_000);
}

public Boolean napTime(long msSleep) throws InterruptedException {
    Thread.sleep(msSleep);
    return true;
}
}
-----------------------------------
@Mock
TimeTools Timetools;

@TEST
public void timeTest() {
when(timeTools.msSince(anyLong()))
            .thenReturn(0l)
            .thenReturn(5_500l)
            .thenReturn(11_000l)
            .thenReturn(11_000l)
            .thenReturn(0l)
            .thenReturn(11_000l)
            .thenReturn(11_000l)
            .thenReturn(0l)
            .thenReturn(29_000l);
}

但最好的方法是注入 sleeper 然后模拟它。所以在你的测试中,你实际上不会睡觉。然后您的单元测试将像闪电一样快速运行。

于 2017-04-28T14:21:55.860 回答