How to Write Test Cases in Java Application using Mockito and Junit?
Last Updated :
09 Mar, 2023
Mockito is an open-source testing framework used for unit testing of Java applications. It plays a vital role in developing testable applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in Unit Testing. Unit Testing is a type of software testing in which individual components of the software are tested. The major objective of using the Mockito framework is to simplify the development of a test by mocking external dependencies and using them in the test code. And as a result, Mockito provides a simpler test code that is easier to understand, more readable, and modifiable. Mockito can also be used with other testing frameworks like JUnit and TestNG.
JUnit framework is a Java framework that is also used for testing. Now, JUnit is used as a standard when there is a need to perform testing in Java. So in this article, we will be discussing test cases in a java application using Mockito and Junit.
Step by Step Implementation
Step 1: Create a Maven project in your favorite Java IDE (IHere we are using IntelliJ IDEA)
Step 2: 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:
XML
< dependency >
< groupId >org.mockito</ groupId >
< artifactId >mockito-all</ artifactId >
< version >2.0.2-beta</ version >
</ dependency >
|
Dependency for Junit is as follows:
XML
< dependency >
< groupId >junit</ groupId >
< artifactId >junit</ artifactId >
< version >4.8.2</ version >
</ dependency >
|
Implementation: Below is the complete code for the pom.xml file
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< modelVersion >4.0.0</ modelVersion >
< groupId >org.example</ groupId >
< artifactId >mockito-demo</ artifactId >
< version >1.0-SNAPSHOT</ version >
< dependencies >
< dependency >
< groupId >junit</ groupId >
< artifactId >junit</ artifactId >
< version >4.8.2</ version >
</ dependency >
< dependency >
< groupId >org.mockito</ groupId >
< artifactId >mockito-all</ artifactId >
< version >2.0.2-beta</ version >
</ dependency >
</ dependencies >
< properties >
< maven.compiler.source >11</ maven.compiler.source >
< maven.compiler.target >11</ maven.compiler.target >
</ properties >
</ project >
|
Step 3: Now you have to create one interface named as TodoService and one class named as TodoServiceImpl.
Java
import java.util.List;
public interface TodoService {
public List<String> retrieveTodos(String user);
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class TodoServiceImpl {
private TodoService todoService;
public TodoServiceImpl(TodoService todoService)
{
this .todoService = todoService;
}
public List<String>
retrieveTodosRelatedToJava(String user)
{
List<String> filteredTodos
= new ArrayList<String>();
List<String> todos
= todoService.retrieveTodos(user);
for (String todo : todos) {
if (todo.contains( "Java" )) {
filteredTodos.add(todo);
}
}
return filteredTodos;
}
}
|
Step 4: 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
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith (MockitoJUnitRunner. class )
public class TodoServiceImplMockTest {
TodoServiceImpl todoBusiness;
@Mock TodoService todoServiceMock;
@Before public void setUp()
{
todoBusiness = new TodoServiceImpl(todoServiceMock);
}
@Test
public void testRetrieveTodosRelatedToSpring_usingMock()
{
List<String> todos
= Arrays.asList( "Learn Spring" , "Learn Java" ,
"Learn Spring Boot" );
when(todoServiceMock.retrieveTodos( "User" ))
.thenReturn(todos);
List<String> filteredTodos
= todoBusiness.retrieveTodosRelatedToJava(
"User" );
assertEquals( 1 , filteredTodos.size());
}
@Test
public void
testRetrieveTodosRelatedToSpring_withEmptyList_usingMock()
{
List<String> todos = Arrays.asList();
when(todoServiceMock.retrieveTodos( "Dummy" ))
.thenReturn(todos);
List<String> filteredTodos
= todoBusiness.retrieveTodosRelatedToJava(
"Dummy" );
assertEquals( 0 , filteredTodos.size());
}
}
|
Now you have successfully written two test cases for your java application. Now to run the test cases click on the green button as shown in the below image. And you can see your test cases have been passed.
Share your thoughts in the comments
Please Login to comment...