Showing posts with label Mockito. Show all posts
Showing posts with label Mockito. Show all posts
Wednesday, June 25, 2014
Mockito Cookbook is out!!
I'm very pleased to announce that you can order my new book Mockito Cookbook here https://www.packtpub.com/mockito-cookbook/book . I hope that you will enjoy it :)
Thursday, June 12, 2014
Pre-order Mockito Cookbook!!
Hi!
I'm pleased to announce that you can already pre-order my new book Mockito Cookbook. You can buy it on Packt Publishing's website. The whole code from the book + plenty of extras are available at Github over here. Enjoy :)
Wednesday, June 4, 2014
Mockito Instant Code Repo at Github
Hi!
Since I was asked for the code to the "Mockito Instant" book a couple of times I decided to place it on Github. Here is the link - https://github.com/marcingrzejszczak/mockito-instant. Enjoy :)
Since I was asked for the code to the "Mockito Instant" book a couple of times I decided to place it on Github. Here is the link - https://github.com/marcingrzejszczak/mockito-instant. Enjoy :)
Wednesday, August 28, 2013
Mockito Instant - my book has finally been published!
I am very pleased to announce that my book about Mockito entitled "Mockito Instant" has finally been published! You can buy it at Packt Publishing online shop.
Friday, August 9, 2013
Injecting Test Doubles in Spring using Mockito and BeanPostProcessors
I'm pretty sure that if you have ever used Spring and are familliar with unit testing, you have encountered a problem related to injecting mocks / spies (Test Doubles) in the Spring's application context which you wouldn't want to modify. This article presents an approach how to solve this issue using Spring's components.
Tuesday, August 6, 2013
Spock - return nested spies / mocks
Hi! Some time ago I have written an article about Mockito and using RETURNS_DEEP_STUBS when working with JAXB. Quite recently we have faced a similliar issue with deeply nesetd JAXB and the awesome testing framework written in Groovy called Spock. Natively Spock does not support creating deep stubs or spies so we needed to create a workaround for it and this article will show you how to do it.
Wednesday, June 12, 2013
Mockito - Extra Interfaces with annotations and static methods
In the code I have quite recently came across a really bad piece of code that based on class casting in terms of performing some actions on objects. Of course the code needed to be refactored but sometimes you can't do it / or don't want to do it (and it should be understandable) if first you don't have unit tests of that functionality. In the following post I will show how to test such code, how to refactor it and in fact what I think about such code ;)
Saturday, June 8, 2013
Mockito - RETURNS_DEEP_STUBS for JAXB
Sorry for not having written for some time but I was busy with writing the JBoss Drools Refcard for DZone and I am in the middle of writing a book about Mockito so I don't have too much time left for blogging...
Anyway quite recently on my current project I had an interesting situation regarding unit testing with Mockito and JAXB structures. We have very deeply nested JAXB structures generated from schemas that are provided for us which means that we can't change it in anyway.
Sunday, October 7, 2012
Simulation of time consuming actions in integration tests
Hi!
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 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 :)
Subscribe to:
Posts (Atom)