JUnit and Mockito cooperation
This time I want to make an overview of testing framework Mockito. Definitely this is one of the most popular tools for the testing of java code. I have already made the overview of the Mockito’s competitor – EasyMock. This post will be based on the sample application from the post about EasyMock. I mean classes which represent coffee machine functionality.
Preparation of testing with Mockito
As usually I will use Maven for the project setup. So to make Mockito available in the project, I need to add following dependency in the pom.xml file:
<dependencies> <dependency> <groupid>org.mockito</groupid> <artifactid>mockito-all</artifactid> <version>1.9.5</version> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> </dependency> </dependencies>
Notice that JUnit isn’t required for the normal functionality of Mockito, you can use in the same way TestNG instead of it. So you can use either JUnit or TestNG depending on your decision.
Let’s move further. As I mentioned earlier, all tests in this article will be written for the sample application from the post about EasyMock. You can read just “Application code” section from there. That will be enough to understand the subject of the testing.
Mockito & JUnit tests
I want to start this section with a couple of sentences about the Mockito framework. Definitely with the help of Mockito you can do everything you wish in the testing. You can create mocks, stubs, make verifications and all this with the help of clear API.
It’s time to see how unit tests with Mockito look like.
import static org.junit.Assert.*; import static org.mockito.Mockito.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import com.app.data.Portion; import com.app.exceptions.NotEnoughException; import com.app.mechanism.CoffeeMachine; import com.app.mechanism.interfaces.ICoffeeMachine; import com.app.mechanism.interfaces.IContainer; @RunWith(MockitoJUnitRunner.class) public class CoffeeMachineTest { ICoffeeMachine coffeeMachine; @Mock IContainer coffeeContainer; @Mock IContainer waterContainer; @Before public void setUp() { coffeeMachine = new CoffeeMachine(coffeeContainer, waterContainer); } @After public void tearDown() { coffeeContainer = null; waterContainer = null; coffeeMachine = null; } @Test public void testMakeCoffe() throws NotEnoughException { when(coffeeContainer.getPortion(Portion.LARGE)).thenReturn(true); when(waterContainer.getPortion(Portion.LARGE)).thenReturn(true); assertTrue(coffeeMachine.makeCoffee(Portion.LARGE)); } @Test public void testNotEnoughException() throws NotEnoughException { when(coffeeContainer.getPortion(Portion.SMALL)).thenReturn(false); when(waterContainer.getPortion(Portion.SMALL)).thenReturn(true); assertFalse(coffeeMachine.makeCoffee(Portion.SMALL)); } }
I will explain in a few words, what’s going on in the test class. I’m testing the coffee machine and for this purpose I create mocks for the coffee container and water container. Further in the tests I will define some behavior for the mocks, and depending on this coffee machine will perform expected actions.
... @Mock IContainer coffeeContainer; @Mock IContainer waterContainer; ...
In the code above I declared two mocks. Creation of the mocks in this way requires @RunWith(MockitoJUnitRunner.class) annotation applied to the test class. As the alternative you can use MockitoAnnotations.initMocks(testClass); before any test will run, e.g. you can put this code snippet in the method annotated with @Before.
... when(coffeeContainer.getPortion(Portion.LARGE)).thenReturn(true); ...
Defining mock’s behavior in the Mockito performs in a very convenient manner. You specify what should be returned after the particular method was called. Notice that I have made several static imports in the test class.
Summary
Mockito framework has conquered my heart. It’s very convenient, its API is clear, usage is laconic. Mockito contains a lot of methods which can be needed during the unit tests development. Of course one post is too small to describe all features of Mockito, so I recommend you to dig deeper using follow link to the official documentation.
Very useful article. A good introduction to Mockito, for a newcomer like me.