在集成测试中模拟耗时的动作
生活随笔
收集整理的這篇文章主要介紹了
在集成测试中模拟耗时的动作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近在我的一個項目中,我遇到一種情況,需要為該應用程序創建集成測試。 這不是很奇怪,不是嗎? 有趣的是,該應用程序的邏輯涉及一些并發問題,并且其中一個組件必須連接到外部服務,這將花費幾秒鐘的事實。 由于在集成測試中不需要進行實際的連接,因此需要對組件進行模擬。 模擬耗時的動作呢? 好吧,讓我們來看看我的做法…
任務。
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()結尾。 這就是我在集成測試中設法創建一個模擬等待服務完成的方式。 如果您有其他想法或意見,請隨時在下面發表評論
翻譯自: https://www.javacodegeeks.com/2013/04/simulation-of-time-consuming-actions-in-integration-tests.html
總結
以上是生活随笔為你收集整理的在集成测试中模拟耗时的动作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 8 Lambdas –缺少脱离
- 下一篇: 老项目备案情况说明怎么写(老项目备案情况