N.B. This post has been reposted for educational purpose only, there is no intention for 
infringement of copyright issues(if there any). And conveys proper acknowledgement to the
author. source: http://fruzenshtein.com/

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:

  1.         org.mockito
  2.         mockito-all
  3.         1.9.5
  4.         junit
  5.         junit
  6.         4.11

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.

  1. import static org.junit.Assert.*;
  2. import static org.mockito.Mockito.*;
  3. import org.junit.After;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.mockito.Mock;
  8. import org.mockito.runners.MockitoJUnitRunner;
  9. import com.app.data.Portion;
  10. import com.app.exceptions.NotEnoughException;
  11. import com.app.mechanism.CoffeeMachine;
  12. import com.app.mechanism.interfaces.ICoffeeMachine;
  13. import com.app.mechanism.interfaces.IContainer;
  14. @RunWith(MockitoJUnitRunner.class)
  15. public class CoffeeMachineTest {
  16.     ICoffeeMachine coffeeMachine;
  17.     @Mock
  18.     IContainer coffeeContainer;
  19.     @Mock
  20.     IContainer waterContainer;
  21.     @Before
  22.     public void setUp() {
  23.         coffeeMachine = new CoffeeMachine(coffeeContainer, waterContainer);
  24.     }
  25.     @After
  26.     public void tearDown() {
  27.         coffeeContainer = null;
  28.         waterContainer = null;
  29.         coffeeMachine = null;
  30.     }
  31.     @Test
  32.     public void testMakeCoffe() throws NotEnoughException {
  33.         when(coffeeContainer.getPortion(Portion.LARGE)).thenReturn(true);
  34.         when(waterContainer.getPortion(Portion.LARGE)).thenReturn(true);
  35.         assertTrue(coffeeMachine.makeCoffee(Portion.LARGE));
  36.     }
  37.     @Test
  38.     public void testNotEnoughException() throws NotEnoughException {
  39.         when(coffeeContainer.getPortion(Portion.SMALL)).thenReturn(false);
  40.         when(waterContainer.getPortion(Portion.SMALL)).thenReturn(true);
  41.         assertFalse(coffeeMachine.makeCoffee(Portion.SMALL));
  42.     }
  43. }

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.

  1.     @Mock
  2.     IContainer coffeeContainer;
  3.     @Mock
  4.     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.

  1. 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.