Mockito and JUnit Integration Using Maven Example

By | October 15, 2020

In this post, We will learn How to integrate  Mockito  3 and JUnit  5 using a demo project

Mockito Extension

Mockito provides an implementation for JUnit5 or Jupiter extensions in the library – mockito-junit-jupiter. we have to include the following dependency with test scope  in our pom.xml

Let’s try to understand the above concept using a demo project

pom.xml

 

EmployeeDAO.java

 

EmployeeDAOImpl.java

 

EmpoyeeService.java

 

EmployeeServiceImpl.java

 

DBUtil.java

 

Employee.java

 

ClientTest.java

 

DbScript

Building the Test Class using Junit 5 and Mockito 3

Let’s build our test classes and use the Mockito extension for it.

EmpoyeeServiceTest1.java

 

 

EmpoyeeServiceTest2.java

Now right click on either EmpoyeeServiceTest1.java or EmpoyeeServiceTest2.java class and select Run As then JUnit Test as shown in the below Images (Optionally you can right-click on the project and select Run As then JUnit Test)

The Output of the above project:

You May Also Like:

What is Mocking?
Why Need for mocking?
What are the Benefits of Mockito?
How to mock interface using Mockito example?
@Mock and @Spy Mockito Annotations With Example
@InjectMocks Annotation in Mockito with Example
Mockito – Verifying Method Calls
@Captor Annotation in Mockito with Example
Adding behavior to mocked object in Mockito
Mocking Void Methods with Mockito
Mocking Exception Throwing using Mockito
Mockito’s mock Overloaded Methods

That’s all about Mockito and JUnit Integration Using Maven Example
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

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