JUnit Mockito doReturn Example

In this post, I will be showing one of the most widely used and popular JUnit Testing Mocking framework – Mockito. Mockito is one of the widely used testing API for Java.


The following configurations are required in order to run the application

JDK 1.7
Have maven installed and configured
Junit, Mockito, PowerMockito dependency in pom.xml
Now we will see the below steps how to create a maven based Java project in Eclipse

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.


Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : junit

Step 2. Modify the pom.xml file as shown below.

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.7 from the next window. Click on Finish then Ok.

Step 4. Create a Model class

Step 5. Create service interface

Step 6. Create dao interface

Step 7. Create service implementation class

Step 8. Create dao implementation class

Step 9. Create Junit class

Step 10. Now run the Junit class. You will see that there is no error.


Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.