Introduction
Mocking is a technique that allows testing of a class’s functionality in total isolation. Additionally, Mocking does not require property file read, file server read, or even a database connection to test the functionality.
Mockito makes creating mock objects straightforward as it uses Java Reflection to create mock objects for an interface.
This blog focuses on helping you understand how to create a mock.
Using Mockito.mock()
The Mockito.mock() method enables us to create a mock object of a class or an interface.
We can then use the mock to return values for its methods and verify if they were called.
Mock objects. mock() creates mocks without bothering about the order of the method calls that mock is going to make while in action.
Step 1 − Create an interface called Calculator to provide mathematical functions
File: Calculator.java
public interface Calculator {
public double add(double inp1, double inp2);
public double subtract(double inp1, double inp2);
public double multiply(double inp1, double inp2);
public double divide(double inp1, double inp2);
}
Step 2 − Create a JAVA class to represent mathApp
File: mathApp.java
public class mathApp {
private Calculator calc;
public void setCalculator(Calculator calc){
this.calc = calc;
}
public double add(double inp1, double inp2){
return calc.add(inp1, inp2);
}
public double subtract(double inp1, double inp2){
return calc.subtract(inp1, inp2);
}
public double multiply(double inp1, double inp2){
return calc.multiply(inp1, inp2);
}
public double divide(double inp1, double inp2){
return calc.divide(inp1, inp2);
}
}
Step 3 − Test the mathApp class.
Let's test the mathApp class by injecting a mock of Calculator in it. Mockito will create a mock.
Here we have appended two mock method calls, add() and subtract(), to the mock object via when(). However, we've called subtract() during testing before we call add(). When a mock object is made using create(), the order of execution does not matter.
File: MathsAppSection .java
package com.tutorialspoint.mock;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
// @RunWith will attach a runner with the test class to initialize the test data
@RunWith(MockitoJUnitRunner.class)
public class MathsAppSection {
private mathApp mathApp;
private Calculator calc;
@Before
public void setUp(){
mathApp = new mathApp();
calc = mock(Calculator.class);
mathApp.setCalculator(calc);
}
@Test
public void testAddAndSubtract(){
//adding the behavior to add numbers
when(calc.add(20.0,10.0)).thenReturn(30.0);
//subtracting the behavior to subtract numbers
when(calc.subtract(20.0,10.0)).thenReturn(10.0);
//testing the subtract functionality
Assert.assertEquals(mathApp.subtract(20.0, 10.0),10.0,0);
//testing the add functionality
Assert.assertEquals(mathApp.add(20.0, 10.0),30.0,0);
//verifying call to calc is made or not
verify(calc).add(20.0,10.0);
verify(calc).subtract(20.0,10.0);
}
}
Step 4 − Execute test cases
Next we create a java class file called RunTest in C:\> Mockito_WORKSPACE to execute Test case(s).
File: RunTest.java
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class RunTest {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(MathsAppSection .class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
Step 5 − Verify the Result
Compile the classes using the javac compiler.
C:\Mockito_WORKSPACE>javac Calculator.java mathApp.java MathsAppSection .java RunTest.java
Now run the Test Runner to get the result.
C:\Mockito_WORKSPACE>java RunTest
Verify the output.
true




