About Vinod Kumar Kashyap

Vinod is Sun Certified and love to work in Java and related technologies. Having more than 10 years of experience, he had developed software’s including technologies like Java, Hibernate, Struts, Spring, HTML 5, jQuery, CSS, Web Services, MongoDB, AngularJS. He is also a JUG Leader of Chandigarh Java User Group.

JUnit Mockito When thenReturn Example

In this example we will show you the usage of JUnit Mockito When thenReturn method. JUnit has many capabilities for testing the unit test cases for Java. But sometimes it lacks some of the common functionalities. Where it lacks and how we can complete, we will be showing in this example.

First of all, we have to be aware of the testing framework we are using here. We are using 2 test frameworks who works jointly to make a complete test scenario. First is JUnit and second is Mockito. They are independent of each other and works independently very well. But we are using here to show how we can leverage the use of both.

1. Introduction

If you are regular reader of my blogs about JUnit, then you are already aware of the JUnit. If not, you can see JUnit Hello World example. In JUnit, it is very cumbersome to test the scenarios where there is a dependency between the classes.

Want to be a JUnit Master ?

Subscribe to our newsletter and download the JUnit Programming Cookbook right now!

In order to help you master unit testing with JUnit, we have compiled a kick-ass guide with all the major JUnit features and use cases! Besides studying them online you may download the eBook in PDF format!

Email address:

Leave this field empty if you're human:

To rescue this, Mockito comes in picture. It is well known testing framework. In Mockito we mock the objects of the class and then do operations on them. Mockito makes heavy use of static methods. It is good to use static imports to make code shorter and more readable. IDE can be used to automatize adding static imports.

Tip
The when(...).thenReturn(...) method chain is be used to specify a condition and a return value for this condition.

This is very simple model class of an Employee with getters and setters. Next, we will create a DAO class, that we will be used as a mock for testing. We have created a Singleton class. What is singleton class and why it is used? To know the answer you can visit Singleton Example.

4.2 Test Class

Now, we will create the main class that will be used to test all the cases.

Tip
Mockito, by default, compares arguments using equals () methods.

To get annotations to function, you need to either call MockitoAnnotations.initMocks( testClass ) (usually in a @Before method ) or use MockitoJUnit4Runner as a JUnit runner. In this example we will be using @RunWith(MockitoJUnitRunner.class).

Examining the class.Line 16: We are using @RunWith(MockitoJUnitRunner.class), so that our program will run with Mockito.Line 23: @BeforeClass annotation of JUnit is used for initial setup. This will run before running any other test case.Line 27: EmployeeDAO mock is being created at this line.Line 30, 33: New Employee objects are being createdLine 36-39: stubbing is done so that when some condition is matched what needs to be done i.e. we are using the When with thenReturn of the Mockito framework. This is the main crux of the example. This is where we have used our core principal of our example.Line 42,43: multiple calls to same method. We will see the side effects below.Line 49: Chaining of stubs.

To run this example, right click on project in eclipse, Run As -> Maven test.

Or, if you run the EmployeeTest.java by right clicking on class and then Run As -> JUnit Test, you will following output.

Figure 3: JUnit Mockito When thenReturn output

All our test cases have passed.

5. Other use cases of When thenReturn

Let’s see some more ways in which we can use the when(...).thenReturn(...) method.

Tip
Once stubbed, the method will always return a stubbed value, regardless of how many times it is called. Last stubbing is more important – when you stubbed the same method with the same arguments many times.

For example:
In our class EmployeeTest.java at line no 43 (when(mockEmployeeDAO.deleteEmployee(1001L)).thenReturn("REMOVED");). If you call this 10 times it will always return same value.
But if we want to return different values, then we can chain the stubs. See the example below.

6. Conclusion

JUnit Mockito When thenReturn example explains the usage of the special method of Mockito framework. This example explains the usage of the when(...).thenReturn(...) method and the scenarios in which it is used.

Leave a Reply

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.