14 min read

(For more resources related to this topic, see here.)

Mockito is an open source framework for Java that allows you to easily create test doubles (mocks). What makes Mockito so special is that it eliminates the common expect-run-verify pattern (which was present, for example, in EasyMock—please refer to http://monkeyisland.pl/2008/02/24/can-i-test-what-i-want-please for more details) that in effect leads to a lower coupling of the test code to the production code as such. In other words, one does not have to define the expectations of how the mock should behave in order to verify its behavior. That way, the code is clearer and more readable for the user.

On one hand, Mockito has a very active group of contributors and is actively maintained. On the other hand, by the time this article is written, the last Mockito release (Version 1.9.5) would have been in October 2012.

You may ask yourself the question, “Why should I even bother to use Mockito in the first place?” Out of many, Mockito offers the following key features:

There is no expectation phase for Mockito—you can either stub or verify the mock’s behavior

You are able to mock both interfaces and classes

You can produce little boilerplate code while working with Mockito by means of annotations

You can easily verify or stub with intuitive argument matchers

Before diving into Mockito as such, one has to understand the concept behind System Under Test (SUT) and test doubles. We will base on what Gerard Meszaros has defined in the xUnit Patterns (http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html).

SUT (http://xunitpatterns.com/SUT.html) describes the system that we are testing. It doesn’t have to necessarily signify a class but any part of the application that we are testing or even the whole application as such.

As for test doubles (http://www.martinfowler.com/bliki/TestDouble.html), it’s an object that is used only for testing purposes, instead of a real object. Let’s take a look at different types of test doubles:

Dummy : This is an object that is used only for the code to compile—it doesn’t have any business logic (for example, an object passed as a parameter to a method)

: This is an object that is used only for the code to compile—it doesn’t have any business logic (for example, an object passed as a parameter to a method) Fake : This is an object that has an implementation but it’s not production ready (for example, using an in-memory database instead of communicating with a standalone one)

: This is an object that has an implementation but it’s not production ready (for example, using an in-memory database instead of communicating with a standalone one) Stub : This is an object that has predefined answers to method executions made during the test

: This is an object that has predefined answers to method executions made during the test Mock : This is an object that has predefined answers to method executions made during the test and has recorded expectations of these executions

: This is an object that has predefined answers to method executions made during the test and has recorded expectations of these executions Spy: These are objects that are similar to stubs, but they additionally record how they were executed (for example, a service that holds a record of the number of sent messages)

An additional remark is also related to testing the output of our application. The more decoupled your test code is from your production code, the better since you will have to spend less time (or even none) on modifying your tests after you change the implementation of the code.

Coming back to the article’s content—this article is all about getting started with Mockito. We will begin with how to add Mockito to your classpath. Then, we’ll see a simple setup of tests for both JUnit and TestNG test frameworks. Next, we will check why it is crucial to assert the behavior of the system under test instead of verifying its implementation details. Finally, we will check out some of Mockito’s experimental features, adding hints and warnings to the exception messages. The very idea of the following recipes is to prepare your test classes to work with Mockito and to show you how to do this with as little boilerplate code as possible.

Due to my fondness of the behavior driven development (http://dannorth.net/introducing-bdd/ first introduced by Dan North), I’m using Mockito’s BDDMockito and AssertJ’s BDDAssertions static methods to make the code even more readable and intuitive in all the test cases. Also, please read Szczepan Faber’s blog (author of Mockito) about the given, when, then separation in your test methods—http://monkeyisland.pl/2009/12/07/given-when-then-forever/—since these are omnipresent throughout the article.

I don’t want the article to become a duplication of the Mockito documentation, which is of high quality—I would like you to take a look at good tests and get acquainted with Mockito syntax from the beginning. What’s more, I’ve used static imports in the code to make it even more readable, so if you get confused with any of the pieces of code, it would be best to consult the repository and the code as such.

Adding Mockito to a project’s classpath

Adding Mockito to a project’s classpath is as simple as adding one of the two jars to your project’s classpath:

mockito-all : This is a single jar with all dependencies (with the hamcrest and objenesis libraries—as of June 2011).

: This is a single jar with all dependencies (with the and libraries—as of June 2011). mockito-core: This is only Mockito core (without hamcrest or objenesis). Use this if you want to control which version of hamcrest or objenesis is used.

How to do it…

If you are using a dependency manager that connects to the Maven Central Repository, then you can get your dependencies as follows (examples of how to add mockito-all to your classpath for Maven and Gradle):

For Maven, use the following code:

org.mockito mockito-all 1.9.5 test

For Gradle, use the following code:

testCompile "org.mockito:mockito-all:1.9.5"

If you are not using any of the dependency managers, you have to either download mockito-all.jar or mockito-core.jar and add it to your classpath manually (you can download the jars from https://code.google.com/p/mockito/downloads/list).

Getting started with Mockito for JUnit

Before going into details regarding Mockito and JUnit integration, it is worth mentioning a few words about JUnit.

JUnit is a testing framework (an implementation of the xUnit famework) that allows you to create repeatable tests in a very readable manner. In fact, JUnit is a port of Smalltalk’s SUnit (both the frameworks were originally implemented by Kent Beck). What is important in terms of JUnit and Mockito integration is that under the hood, JUnit uses a test runner to run its tests (from xUnit—test runner is a program that executes the test logic and reports the test results).

Mockito has its own test runner implementation that allows you to reduce boilerplate in order to create test doubles (mocks and spies) and to inject them (either via constructors, setters, or reflection) into the defined object. What’s more, you can easily create argument captors. All of this is feasible by means of proper annotations as follows:

@Mock : This is used for mock creation

: This is used for mock creation @Spy : This is used to create a spy instance

: This is used to create a spy instance @InjectMocks : This is used to instantiate the @InjectMock annotated field and inject all the @Mock or @Spy annotated fields into it (if applicable)

: This is used to instantiate the annotated field and inject all the or annotated fields into it (if applicable) @Captor: This is used to create an argument captor

By default, you should profit from Mockito’s annotations to make your code look neat and to reduce the boilerplate code in your application.

Getting ready

In order to add JUnit to your classpath, if you are using a dependency manager that connects to the Maven Central Repository, then you can get your dependencies as follows (examples for Maven and Gradle):

To add JUnit in Maven, use the following code:

junit junit 4.11 test

To add JUnit in Gradle, use the following code:

testCompile('junit:junit:4.11')

If you are not using any of the dependency managers, you have to download the following jars:

junit.jar

hamcrest-core.jar

Add the downloaded files to your classpath manually (you can download the jars from https://github.com/junit-team/junit/wiki/Download-and-Install).

For this recipe, our system under test will be a MeanTaxFactorCalculator class that will call an external service, TaxService, to get the current tax factor for the current user. It’s a tax factor and not tax as such since, for simplicity, we will not be using BigDecimals but doubles, and I’d never suggest using doubles to anything related to money, as follows:

public class MeanTaxFactorCalculator { private final TaxService taxService; public MeanTaxFactorCalculator(TaxService taxService) { this.taxService = taxService; } public double calculateMeanTaxFactorFor(Person person) { double currentTaxFactor = taxService.getCurrentTaxFactorFor(person); double anotherTaxFactor = taxService.getCurrentTaxFactorFor(person); return (currentTaxFactor + anotherTaxFactor) / 2; } }

How to do it…

To use Mockito’s annotations, you have to perform the following steps:

Annotate your test with the @RunWith(MockitoJUnitRunner.class). Annotate the test fields with the @Mock or @Spy annotation to have either a mock or spy object instantiated. Annotate the test fields with the @InjectMocks annotation to first instantiate the @InjectMock annotated field and then inject all the @Mock or @Spy annotated fields into it (if applicable).

The following snippet shows the JUnit and Mockito integration in a test class that verifies the SUT’s behavior (remember that I’m using BDDMockito.given(…) and AssertJ’s BDDAssertions.then(…) static methods:

@RunWith(MockitoJUnitRunner.class) public class MeanTaxFactorCalculatorTest { static final double TAX_FACTOR = 10; @Mock TaxService taxService; @InjectMocks MeanTaxFactorCalculator systemUnderTest; @Test public void should_calculate_mean_tax_factor() { // given given(taxService.getCurrentTaxFactorFor(any(Person.class)))

.willReturn(TAX_FACTOR); // when double meanTaxFactor = systemUnderTest.

calculateMeanTaxFactorFor(new Person()); // then then(meanTaxFactor).isEqualTo(TAX_FACTOR); } }

To profit from Mockito’s annotations using JUnit, you just have to annotate your test class with @RunWith(MockitoJUnitRunner.class).

How it works…

The Mockito test runner will adapt its strategy depending on the version of JUnit. If there exists a org.junit.runners.BlockJUnit4ClassRunner class, it means that the codebase is using at least JUnit in Version 4.5.What eventually happens is that the MockitoAnnotations.initMocks(…) method is executed for the given test, which initializes all the Mockito annotations (for more information, check the subsequent There’s more… section).

There’s more…

You may have a situation where your test class has already been annotated with a @RunWith annotation and seemingly, you may not profit from Mockito’s annotations. In order to achieve this, you have to call the MockitoAnnotations.initMocks method manually in the @Before annotated method of your test, as shown in the following code:

public class MeanTaxFactorCalculatorTest { static final double TAX_FACTOR = 10; @Mock TaxService taxService; @InjectMocks MeanTaxFactorCalculator systemUnderTest; @Before public void setup() { MockitoAnnotations.initMocks(this); } @Test public void should_calculate_mean_tax_factor() { // given given(taxService.getCurrentTaxFactorFor(Mockito.any(Person.class

))).willReturn(TAX_FACTOR); // when double meanTaxFactor = systemUnderTest.calculateMeanTaxFactorFor

(new Person()); // then then(meanTaxFactor).isEqualTo(TAX_FACTOR); } }

To use Mockito’s annotations without a JUnit test runner, you have to call the MockitoAnnotations.initMocks method and pass the test class as its parameter.

Mockito checks whether the user has overridden the global configuration of AnnotationEngine, and if this is not the case, the InjectingAnnotationEngine implementation is used to process annotations in tests. What is done internally is that the test class fields are scanned for annotations and proper test doubles are initialized and injected into the @InjectMocks annotated object (either by a constructor, property setter, or field injection, in that precise order).

You have to remember several factors related to the automatic injection of test doubles as follows:

If Mockito is not able to inject test doubles into the @InjectMocks annotated fields through either of the strategies, it won’t report failure—the test will continue as if nothing happened (and most likely, you will get NullPointerException ).

annotated fields through either of the strategies, it won’t report failure—the test will continue as if nothing happened (and most likely, you will get ). For constructor injection, if arguments cannot be found, then null is passed

For constructor injection, if nonmockable types are required in the constructor, then the constructor injection won’t take place.

For other injection strategies, if you have properties with the same type (or same erasure) and if Mockito matches mock names with a field/property name, it will inject that mock properly. Otherwise, the injection won’t take place.

For other injection strategies, if the @InjectMocks annotated object wasn’t previously initialized, then Mockito will instantiate the aforementioned object using a no-arg constructor if applicable.

See also

JUnit documentation at https://github.com/junit-team/junit/wiki

Martin Fowler’s article on xUnit at http://www.martinfowler.com/bliki/Xunit.html

Gerard Meszaros’s xUnit Test Patterns at http://xunitpatterns.com/

at http://xunitpatterns.com/ @InjectMocks Mockito documentation (with description of injection strategies) at http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/InjectMocks.html

Getting started with Mockito for TestNG

Before going into details regarding Mockito and TestNG integration, it is worth mentioning a few words about TestNG.

TestNG is a unit testing framework for Java that was created, as the author defines it on the tool’s website (refer to the See also section for the link), out of frustration for some JUnit deficiencies. TestNG was inspired by both JUnit and TestNG and aims at covering the whole scope of testing—from unit, through functional, integration, end-to-end tests, and so on. However, the JUnit library was initially created for unit testing only.

The main differences between JUnit and TestNG are as follows:

The TestNG author disliked JUnit’s approach of having to define some methods as static to be executed before the test class logic gets executed (for example, the @BeforeClass annotated methods)—that’s why in TestNG you don’t have to define these methods as static

annotated methods)—that’s why in TestNG you don’t have to define these methods as static TestNG has more annotations related to method execution before single tests, suites, and test groups

TestNG annotations are more descriptive in terms of what they do; for example, the JUnit’s @Before versus TestNG’s @BeforeMethod

Mockito in Version 1.9.5 doesn’t provide any out-of-the-box solution to integrate with TestNG in a simple way, but there is a special Mockito subproject for TestNG (refer to the See also section for the URL) that should be part one of the subsequent Mockito releases. In the following recipe, we will take a look at how to profit from that code and that very elegant solution.

Getting ready

When you take a look at Mockito’s TestNG subproject on the Mockito GitHub repository, you will find that there are three classes in the org.mockito.testng package, as follows:

MockitoAfterTestNGMethod

MockitoBeforeTestNGMethod

MockitoTestNGListener

Unfortunately, until this project eventually gets released you have to just copy and paste those classes to your codebase.

How to do it…

To integrate TestNG and Mockito, perform the following steps:

Copy the MockitoAfterTestNGMethod, MockitoBeforeTestNGMethod, and MockitoTestNGListener classes to your codebase from Mockito’s TestNG subproject. Annotate your test class with @Listeners(MockitoTestNGListener.class). Annotate the test fields with the @Mock or @Spy annotation to have either a mock or spy object instantiated. Annotate the test fields with the @InjectMocks annotation to first instantiate the @InjectMock annotated field and inject all the @Mock or @Spy annotated fields into it (if applicable). Annotate the test fields with the @Captor annotation to make Mockito instantiate an argument captor.

Now let’s take a look at this snippet that, using TestNG, checks whether the mean tax factor value has been calculated properly (remember that I’m using the BDDMockito.given(…) and AssertJ’s BDDAssertions.then(…) static methods:

@Listeners(MockitoTestNGListener.class) public class MeanTaxFactorCalculatorTestNgTest { static final double TAX_FACTOR = 10; @Mock TaxService taxService; @InjectMocks MeanTaxFactorCalculator systemUnderTest; @Test public void should_calculate_mean_tax_factor() { // given given(taxService.getCurrentTaxFactorFor(any(Person.class))

).willReturn(TAX_FACTOR); // when double meanTaxFactor = systemUnderTest.calculateMeanTaxFactorFor

(new Person()); // then then(meanTaxFactor).isEqualTo(TAX_FACTOR); } }

How it works…

TestNG allows you to register custom listeners (your listener class has to implement the IInvokedMethodListener interface). Once you do this, the logic inside the implemented methods will be executed before and after every configuration and test methods get called. Mockito provides you with a listener whose responsibilities are as follows:

Initialize mocks annotated with the @Mock annotation (it is done only once)

annotation (it is done only once) Validate the usage of Mockito after each test method

Remember that with TestNG, all mocks are reset (or initialized if it hasn’t already been done so) before any TestNG method!

See also