在集成测试中模拟耗时的动作
最近在我的一個(gè)項(xiàng)目中,我遇到一種情況,需要為該應(yīng)用程序創(chuàng)建集成測(cè)試。 這不是很奇怪,不是嗎? 有趣的是,該應(yīng)用程序的邏輯涉及一些并發(fā)問題,并且其中一個(gè)組件必須連接到外部服務(wù),這將花費(fèi)幾秒鐘的事實(shí)。 由于在集成測(cè)試中不需要進(jìn)行實(shí)際的連接,因此需要對(duì)組件進(jìn)行模擬。 模擬耗時(shí)的動(dòng)作呢? 好吧,讓我們來看看我的做法…
任務(wù)。
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;}}集成測(cè)試。
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();} }并輸出到控制臺(tái):
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讓我們仔細(xì)看看其中最重要的部分,在其中創(chuàng)建用于執(zhí)行服務(wù)的答案
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(); 這段代碼更改了給定對(duì)象在給定方法執(zhí)行時(shí)應(yīng)執(zhí)行的默認(rèn)操作。 在這種特殊情況下,我們必須模擬一個(gè)返回void的方法-這就是為什么我們從doAnswer(...)開始并以when(...)。process()結(jié)尾。 這就是我在集成測(cè)試中設(shè)法創(chuàng)建一個(gè)模擬等待服務(wù)完成的方式。 如果您有其他想法或意見,請(qǐng)隨時(shí)在下面發(fā)表評(píng)論
翻譯自: https://www.javacodegeeks.com/2013/04/simulation-of-time-consuming-actions-in-integration-tests.html
總結(jié)
以上是生活随笔為你收集整理的在集成测试中模拟耗时的动作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 8 Lambdas –缺少脱离
- 下一篇: 摆脱困境:向REST API添加验证