一学一做征文网站,vs做网站mvc,设计说明怎么写200字,本地购物平台模拟耗时操作最近在我的一个项目中#xff0c;我遇到一种情况#xff0c;需要为该应用程序创建集成测试。 这不是很奇怪#xff0c;不是吗#xff1f; 有趣的是#xff0c;应用程序的逻辑涉及一些并发问题#xff0c;并且其中一个组件必须连接到外部服务#xff0c;这将… 模拟耗时操作 最近在我的一个项目中我遇到一种情况需要为该应用程序创建集成测试。 这不是很奇怪不是吗 有趣的是应用程序的逻辑涉及一些并发问题并且其中一个组件必须连接到外部服务这将花费几秒钟。 由于在集成测试中不需要进行实际的连接因此需要对组件进行模拟。 模拟耗时的动作呢 好吧让我们来看看我的做法... 任务。 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 AnswerObject() {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 AnswerObject() {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模拟耗时操作