In this post, We will learn How to mock void methods with Mockito
Usually most of the time Mockito’s org.mockito.stubbing.Stubber.when(T) method is really good enough to mock an object’s behavior but when we need to mock a void method then we can’t use when(T) method.
Mockito Mock Void Method
Mockito provides the following methods that can be used to mock void methods.
- org.mockito.Mockito.doAnswer(Answer): We can use this method in Mockito when we want to stub a void method with generic
Answer
org.mockito.Mockito.doThrow(Throwable...)
: We can use this method when we want to stub a void method with an exception- org.mockito.Mockito.doNothing(): We can use this method for setting void methods to do nothing. We should be beware that the void method on mocks does nothing by default. However, there are very rare situations when the doNothing() method comes handy
- org.mockito.Mockito.doCallRealMethod(): We can use
this method
when we want to call the real implementation of a method
Mockito mock void method example
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<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>MockingVoidMethodsWithMockitoExample</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> |
Dictionary.java
1 2 3 4 5 6 7 |
package com.kkjavatutorials; public interface Dictionary { public abstract void add(String word, String meaning); public abstract String getMeaning(String word); } |
WordDictionary.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.kkjavatutorials; import java.util.HashMap; import java.util.Map; public class WordDictionary implements Dictionary { private Map<String, String> wordsMap; public WordDictionary() { wordsMap = new HashMap<String, String>(); } public void add(String word, String meaning) { wordsMap.put(word, meaning); } public String getMeaning(String word) { return wordsMap.get(word); } } |
WordDictionaryTest.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 64 65 66 67 68 69 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; /** * Mocking Void Methods with Mockito Examples * @author KK JavaTutorials */ public class WordDictionaryTest { @Test public void whenAddCalledWithVerify() { WordDictionary wordDictionary = mock(WordDictionary.class); doNothing().when(wordDictionary).add(anyString(), anyString()); wordDictionary.add("awesome", "Very Good"); verify(wordDictionary, times(1)).add("awesome", "Very Good"); System.out.println(wordDictionary.getMeaning("awesome")); } @Test public void whenAddCalledValueCaptured() { WordDictionary wordDictionary = mock(WordDictionary.class); ArgumentCaptor<String> valueCapture = ArgumentCaptor.forClass(String.class); doNothing().when(wordDictionary).add(anyString(), valueCapture.capture()); wordDictionary.add("awesome", "Very Good"); assertEquals("Very Good", valueCapture.getValue()); } @Test public void whenAddCalledRealMethodUsingSpy() { WordDictionary wordDictionary = spy(WordDictionary.class); doCallRealMethod().when(wordDictionary).add(anyString(),anyString()); wordDictionary.add("awesome", "Very Good"); verify(wordDictionary, times(1)).add("awesome", "Very Good"); assertEquals("Very Good", wordDictionary.getMeaning("awesome")); //System.out.println(wordDictionary.getMeaning("awesome")); } /** * Test for void Return Type method with Exception */ @Test public void voidMethodThrowingExceptionMockitoTest() { WordDictionary wordDictionary = mock(WordDictionary.class); doThrow(new IllegalStateException("Error occurred!!")) .when(wordDictionary) .add(anyString(), anyString()); assertThrows(IllegalStateException.class, ()->wordDictionary.add("awesome", "Very Good")); } } |
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 Exception Throwing using Mockito
Mockito’s mock Overloaded Methods
That’s all about Mocking Void Methods with Mockito
If you have any feedback or suggestion please feel free to drop in below comment box.