Mockito JUnit Integration - Mockito

How to integrate JUnit and Mockito together?

This chapter will explain about how to integrate JUnit and Mockito together. For this we need to create a Math Application which uses CalculatorService to perform basic mathematical operations such as addition, subtraction, multiply, and division.

We'll use Mockito to mock the dummy implementation of CalculatorService. Along with this, using annotations will helps to showcase their compatibility with both JUnit and Mockito.

Junit integration process is explained here in a step-by-step manner.

Step 1 − Create an interface called CalculatorService to provide mathematical functions

File: CalculatorService.java

Step 2 − Create a JAVA class to represent MathApplication

File: MathApplication.java

Step 3 − Test the MathApplication class

Now,test the MathApplication class, by injecting in it a mock of calculatorService.

File: MathApplicationTester.java

Step 4 − Create a class to execute to test cases

Create a java class file named TestRunner in C> Mockito_WORKSPACE to execute Test case(s).

File: TestRunner.java

Step 5 − Verify the Result

Compile the classes using javac compiler as follows –

Now run the Test Runner to see the result –

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Mockito Topics