In this post, We will learn about @InjectMocks Annotation in Mockito with Example
Mockito’s @InjectMocks annotation usually allows us to inject mocked dependencies in the annotated class mocked object. This is very useful when we have an external dependency in the class want to mock. We can specify the mock objects to be injected using @Mock annotation.
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 26 27 28 |
<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>MockitoInjectMocksAnnotationExample</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>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>3.5.7</version> <scope>test</scope> </dependency> </dependencies> </project> |
Resource interface and class
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); } } |
Test class InjectMocksTest.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 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Map; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import static org.mockito.Mockito.when; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(value = { MockitoExtension.class }) public class InjectMocksTest { @Mock private Map<String, String> wordMap; @InjectMocks private Dictionary wordDictionary = new WordDictionary(); @Test public void injectMocksTest() { when(wordMap.get("awesome")).thenReturn("Very Good"); assertEquals("Very Good", wordDictionary.getMeaning("awesome")); } } |
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
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
Mockito’s mock Overloaded Methods
That’s all about @InjectMocks Annotation in Mockito with Example
If you have any feedback or suggestion please feel free to drop in below comment box.