![]() |
VOOZH | about |
Unit testing is an essential practice in software development that ensures individual components of an application work as expected.
In this article, we will learn step by step how to write unit test cases for a Java application using Mockito and JUnit.
Create a Maven project in your favorite Java IDE (Here we are using IntelliJ IDEA)
When you have successfully created a Maven project you have to add some dependencies in your pom.xml file. We have to add the following dependency in your pom.xml file.
Dependency for Mockito is as follows:
Dependency for Junit is as follows:
Below is the complete code for the pom.xml file
Now you have to create one interface named as TodoService and one class named as TodoServiceImpl.
Example: Java Program to Illustrate TodoService File
Example: Java Program to Illustrate TodoServiceImpl File
Now we are going to perform unit testing for the retrieveTodosRelatedToJava() method that is present inside the TodoServiceImpl.java file. To create the test class follow these steps. At first Right-click inside the TodoServiceImpl.java file.
Then click on the Generate button.
Then click on the Test button.
A pop-up window will be shown like this. Here you can modify your test class name. Also, check the setUp and the method that you want to perform unit testing.
Example: Java Program to Illustrate TodoServiceImplMockTest File