最近在我的一个项目中,我遇到一种情况,需要为该应用程序创建集成测试。 这不是很奇怪,不是吗? 有趣的是,该应用程序的逻辑涉及一些并发问题,并且其中一个组件必须连接到外部服务,这将花费几秒钟的事实。 由于在集成测试中不需要进行实际的连接,因此需要对组件进行模拟。 模拟耗时的动作呢? 好吧,让我们来看看我的做法…
任务。
package pl.grzejszczak.marcin;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** Service that does some things including processing of the external service* * @author marcin* */
public class SomeTask implements Runnable {private static final Logger LOGGER = LoggerFactory.getLogger(SomeTask.class);// Service is injected via a dependency injection systemprivate Processable timeConsumingExternalService;private void methodThatConnectsToExternalServices() {// connects to an external service and spends a couple of seconds thereLOGGER.debug("Before processing");timeConsumingExternalService.process();LOGGER.debug("After processing");// some other things to do}public void run() {methodThatConnectsToExternalServices();}public void setTimeConsumingExternalService(Processable timeConsumingExternalService) {this.timeConsumingExternalService = timeConsumingExternalService;}}
集成测试。
package pl.grzejszczak.marcin;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;public class ServiceIntegrationTest {private static final Logger LOGGER = LoggerFactory.getLogger(ServiceIntegrationTest.class);private ExecutorService executorService = Executors.newCachedThreadPool();private Processable timeConsumingExternalServiceMock = Mockito.mock(Processable.class);private SomeTask someTask = new SomeTask();public ServiceIntegrationTest() {initializeMocks();}private void initializeMocks() {Mockito.doAnswer(new Answer<Object>() {public Object answer(InvocationOnMock invocation) throws Throwable {// Simulation of connection to external servicesLOGGER.debug("Sleeping");Thread.sleep(5000);LOGGER.debug("Stopped Sleeping");return null;}}).when(timeConsumingExternalServiceMock).process();// Inject the mock to the Task - in any possible waysomeTask.setTimeConsumingExternalService(timeConsumingExternalServiceMock);}public void executeTest() {executorService.execute(someTask);}public static void main(String args[]) {ServiceIntegrationTest integrationTest = new ServiceIntegrationTest();integrationTest.executeTest();}
}
并输出到控制台:
2012-10-07 22:42:37,378 DEBUG pl.grzejszczak.marcin.SomeTask:21 Before processing2012-10-07 22:42:37,389 DEBUG pl.grzejszczak.marcin.ServiceIntegrationTest:28 Sleeping2012-10-07 22:42:42,390 DEBUG pl.grzejszczak.marcin.ServiceIntegrationTest:30 Stopped Sleeping2012-10-07 22:42:42,392 DEBUG pl.grzejszczak.marcin.SomeTask:23 After processing
让我们仔细看看其中最重要的部分,在其中创建用于执行服务的答案
Mockito.doAnswer(new Answer<Object>() {public Object answer(InvocationOnMock invocation) throws Throwable {// Simulation of connection to external servicesLOGGER.debug("Sleeping");Thread.sleep(5000);LOGGER.debug("Stopped Sleeping");return null;}}).when(timeConsumingExternalServiceMock).process();
这段代码更改了给定对象在给定方法执行时应执行的默认操作。 在这种特殊情况下,我们必须模拟一个返回void的方法-这就是为什么我们从doAnswer(...)开始并以when(...)。process()结尾。 这就是我在集成测试中设法创建一个模拟等待服务完成的方式。 如果您有其他想法或意见,请随时在下面发表评论
参考:来自我们的JCG合作伙伴 Marcin Grzejszczak(位于Blog上)的 集成测试中耗时行为的模拟, 用于编码成瘾者博客。
翻译自: https://www.javacodegeeks.com/2013/04/simulation-of-time-consuming-actions-in-integration-tests.html