@InjectMocks Annotation in Mockito with Example

By | October 15, 2020

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

Resource interface and class

Dictionary.java

 WordDictionary.java

 

Test class InjectMocksTest.java

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.

Leave a Reply

Your email address will not be published. Required fields are marked *