Mockito adds functionality to a mock object using the when() method. Take a look at the following code snippet.
1 2 3 4 |
MyUtils mock = mock(MyUtils.class); when(mock.add(10, 40)).thenReturn(50); doReturn(50).when(mock).add(10, 40); |
Here we are instructing the Mockito to provide or give a behavior of adding 10 and 40 to the add method of MyUtils and as a result, to return the value of 50
Let’s try to understand the above concept using a demo project
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>MockitoAddingBehaviorExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> </properties> <dependencies> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>3.5.7</version> <scope>test</scope> </dependency> </dependencies> </project> |
MyUtils.java
1 2 3 4 5 6 7 |
package com.kkjavatutorials; public interface MyUtils { public abstract int add(int n1,int n2); public abstract int multiply(int n1,int n2); } |
MyUtilsTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package com.kkjavatutorials; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; import java.util.List; public class MyUtilsTest { @Test void addMockitoSimpleBehaviorTest1() { MyUtils mock = mock(MyUtils.class); when(mock.add(10, 40)).thenReturn(50); doReturn(50).when(mock).add(10, 40); assertEquals(50, mock.add(10, 40)); assertEquals(50, mock.add(10, 40)); } @Test void addMockitoSimpleBehaviorTest2() { MyUtils mock = mock(MyUtils.class); when(mock.multiply(20, 20)).thenReturn(400); assertEquals(400, mock.multiply(20, 20)); } @Test void addMockitoconsecutiveMethodCallsTest() { MyUtils mock = mock(MyUtils.class); when(mock.add(anyInt(), anyInt())).thenReturn(60,70,90); assertEquals(60, mock.add(20, 40)); assertEquals(70, mock.add(20, 50)); assertEquals(90, mock.add(50, 40)); } @Test void addMockitoSpyBehaviorTest() { List<String> spy = spy(List.class); doReturn(20).when(spy).size(); assertEquals(20, spy.size()); } @Test void addMockitoBehaviorWithCustomAnswerTest() { List mock = mock(List.class); doAnswer(answer ->"My Answer").when(mock).get(anyInt()); assertEquals("My Answer", mock.get(2)); } } |
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?
Mockito and JUnit Integration Using Maven Example
@Mock and @Spy Mockito Annotations With Example
@InjectMocks Annotation in Mockito with Example
Mockito – Verifying Method Calls
@Captor Annotation in Mockito with Example
Mocking Void Methods with Mockito
Mocking Exception Throwing using Mockito
Mockito’s mock Overloaded Methods
That’s all about Adding behavior to mocked object in Mockito
If you have any feedback or suggestion please feel free to drop in below comment box.