In this post, We will learn How to throw an Exception using Mockito
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 23 24 25 |
<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>ExceptionThrowingUsingMockitoExample</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 70 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import org.junit.jupiter.api.Test; /** * Method Exception Throwing testing using Mockito * @author KK JavaTutorials */ public class WordDictionaryTest { /** * Test for non-void Return Type method with Exception */ @Test public void nonVoidMethodThrowingExceptionMockitoTest() { try { WordDictionary wordDictionary = mock(WordDictionary.class); when(wordDictionary.getMeaning(anyString())) .thenThrow(NullPointerException.class); wordDictionary.getMeaning("awesome"); } catch (Exception e) { System.out.println("Exception occured!!"); assertTrue(e instanceof NullPointerException); } } /** * 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")); } /** * Configuring Spy to throw an exception the same way we've done with the mock */ @Test public void nonVoidMethodThrowingExceptionWithSpyMockitoTest() { String emptyWord =""; try { WordDictionary wordDictionary = new WordDictionary(); WordDictionary spyWordDictionary = spy(wordDictionary); when(spyWordDictionary.getMeaning(emptyWord)) .thenThrow(new IllegalArgumentException("Input is not valid!!")); spyWordDictionary.getMeaning(emptyWord); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); assertEquals("Input is not valid!!",e.getMessage()); } } } |
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
Mockito’s mock Overloaded Methods
That’s all about Mocking Exception Throwing using Mockito
If you have any feedback or suggestion please feel free to drop in below comment box.