DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Simulation of Time Consuming Actions in Integration Tests

Simulation of Time Consuming Actions in Integration Tests

Marcin Grzejszczak user avatar by
Marcin Grzejszczak
·
Nov. 06, 12 · Interview
Like (1)
Save
Tweet
Share
5.96K Views

Join the DZone community and get the full member experience.

Join For Free

Quite recently in one of my projects I had a situation in which I needed to create an integration test for the application. That's not very odd isn't it? :)

What was interesting was the fact that the logic of the app involved some concurrency issues and one of the components had to connect to an external service which would take a couple of seconds. Since in the integration test there was no need to make the actual connection, the component needed to be mocked. What about the simulation of the time consuming action? Well, let's take a look at the way I did it...

The task.

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 system
 private Processable timeConsumingExternalService;
 
 private void methodThatConnectsToExternalServices() {
  // connects to an external service and spends a couple of seconds there
  LOGGER.debug("Before processing");
  timeConsumingExternalService.process();
  LOGGER.debug("After processing");
  // some other things to do
 }
 
 public void run() {
  methodThatConnectsToExternalServices();
 }
 
 public Processable getTimeConsumingExternalService() {
  return timeConsumingExternalService;
 }
 
 public void setTimeConsumingExternalService(Processable timeConsumingExternalService) {
  this.timeConsumingExternalService = timeConsumingExternalService;
 }
 
}


The integration test.

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 services
    LOGGER.debug("Sleeping");
    Thread.sleep(5000);
    LOGGER.debug("Stopped Sleeping");
    return null;
   }
  }).when(timeConsumingExternalServiceMock).process();
  // Inject the mock to the Task - in any possible way
  someTask.setTimeConsumingExternalService(timeConsumingExternalServiceMock);
 }
 
 public void executeTest() {
  executorService.execute(someTask);
 }
 
 public static void main(String args[]) {
  ServiceIntegrationTest integrationTest = new ServiceIntegrationTest();
  integrationTest.executeTest();
 }
}


And the output to the console:

2012-10-07 22:42:37,378 DEBUG pl.grzejszczak.marcin.SomeTask:21 Before processing
2012-10-07 22:42:37,389 DEBUG pl.grzejszczak.marcin.ServiceIntegrationTest:28 Sleeping
2012-10-07 22:42:42,390 DEBUG pl.grzejszczak.marcin.ServiceIntegrationTest:30 Stopped Sleeping
2012-10-07 22:42:42,392 DEBUG pl.grzejszczak.marcin.SomeTask:23 After processing

Let's take a closer look at the most important part in which an Answer for the execution of the service is being created

Mockito.doAnswer(new Answer<Object>() {
   public Object answer(InvocationOnMock invocation) throws Throwable {
    // Simulation of connection to external services
    LOGGER.debug("Sleeping");
    Thread.sleep(5000);
    LOGGER.debug("Stopped Sleeping");
    return null;
   }
  }).when(timeConsumingExternalServiceMock).process();

This piece of code changes the default action that should be done by the given object on a given method execution. In this particular case we had to mock a method that returns void - that's why we start with doAnswer(...) and finish with when(...).process().

That is how inside the integration test I managed to create a simulation of waiting for the service to finish. If you have any ideas or comments on how you would do it in another way please feel free to post a comment below :)

integration test

Published at DZone with permission of Marcin Grzejszczak, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The ChatGPT Model: A Real-Life Example
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Unleashing the Power of JavaScript Modules: A Beginner’s Guide
  • Leaders Make Their Own Problems

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: