In this post, We will talk and learn about different overloaded mock() methods of org.mockito.Mockito class
Mockito’s class org.mockito.Mockito provides below overloaded mock Methods:
public static <T> T mock(Class<T> classToMock): It creates mock object of given class or interface.
public static <T> T mock(Class<T> classToMock, String name) : In this method we can specify mock name. mock name can be helpful for debugging in all verification errors.
public static <T> T mock(Class<T> classToMock, Answer defaultAnswer) : This method creates mock with a specified strategy for its answers to interactions. It’s quite an advanced feature and typically we don’t need it to write general tests. However, it can be helpful when we work with legacy systems.
public static <T> T mock(Class<T> classToMock, MockSettings mockSettings) : This method creates a mock with some non-standard settings.The number of configuration points for a mock grows so we need a fluent way to use new configuration without adding lot of overloaded Mockito.mock() methods.
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>MockitoMocksMethodExample</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> |
MyList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kkjavatutorials; import java.util.AbstractList; public class MyList extends AbstractList<String> { @Override public String get(int index) { return null; } @Override public int size() { return 10; } } |
MyCustomAnswer.java
1 2 3 4 5 6 7 8 9 10 11 |
package com.kkjavatutorials; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; public class MyCustomAnswer implements Answer<Boolean> { public Boolean answer(InvocationOnMock invocation) throws Throwable { return false; } } |
MockitoMocksTest,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 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; import org.mockito.MockSettings; public class MockitoMocksTest { @Test void simplemockTest() { MyList mockMyList = mock(MyList.class); boolean added = mockMyList.add("KK"); when(mockMyList.add(anyString())).thenReturn(false); verify(mockMyList).add(anyString()); assertEquals(false, added); } @Test void mockingWithNameTest() { MyList mockMyList = mock(MyList.class,"Mocking MyList"); boolean added = mockMyList.add("KK"); when(mockMyList.add(anyString())).thenReturn(false); verify(mockMyList).add(anyString()); assertEquals(false, added); } @Test void mockingWithAnswerTest() { MyList mockMyList = mock(MyList.class,new MyCustomAnswer()); boolean added = mockMyList.add("KK"); when(mockMyList.add(anyString())).thenReturn(false); verify(mockMyList).add(anyString()); assertEquals(false, added); } @Test void mockingWithMockSettingsTest() { MockSettings mockSettings = withSettings().defaultAnswer(new MyCustomAnswer()); MyList mockMyList = mock(MyList.class,mockSettings); boolean added = mockMyList.add("KK"); when(mockMyList.add(anyString())).thenReturn(false); verify(mockMyList).add(anyString()); assertEquals(false, added); } } |
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
Adding behavior to mocked object in Mockito
Mocking Void Methods with Mockito
Mocking Exception Throwing using Mockito
That’s all about Mockito’s mock Methods
If you have any feedback or suggestion please feel free to drop in below comment box.