1. Introduction
Mockito is one of the most popular testing frameworks that allows creating mocked and spied objects to verify the behavior of the tested class. In this article, we are going to present @InjectMocks annotations that are used to inject all mocked objects into the testing class.
2. Using @InjectMocks for dependency injection
The @InjectMocks annotation is used to insert all dependencies into the test class. Mockito can inject mocks using constructor injection, setter injection, or property injection.
Let's check a simple example:
We have a simple POJO class that holds Post
data with the following structure:
package com.frontbackend.libraries.mockito.model;
public class Post {
private final String title;
private final String content;
public Post(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
}
The DBConnection
class is responsible for opening and closing database connection:
package com.frontbackend.libraries.mockito.db;
public class DBConnection {
public void open() {
}
public void close() {
}
}
Finally, PostResource
will be our main class for testing:
package com.frontbackend.libraries.mockito.resources;
import com.frontbackend.libraries.mockito.db.DBConnection;
import com.frontbackend.libraries.mockito.model.Post;
import java.util.Arrays;
import java.util.List;
public class PostResource {
private final DBConnection dbConnection;
public PostResource(DBConnection dbConnection) {
this.dbConnection = dbConnection;
}
public List<Post> getPosts() {
try {
dbConnection.open();
return Arrays.asList(
new Post("title1", "content1"),
new Post("title2", "content2"),
new Post("title3", "content3"));
} finally {
dbConnection.close();
}
}
}
In the following example, we used @InjectMocks to inject the DBConnection
mocked object into the PostResource
object.
package com.frontbackend.libraries.mockito;
import static org.mockito.Mockito.verify;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import com.frontbackend.libraries.mockito.db.DBConnection;
import com.frontbackend.libraries.mockito.model.Post;
import com.frontbackend.libraries.mockito.resources.PostResource;
@RunWith(MockitoJUnitRunner.class)
public class MockitoInjectMocksTest {
@Mock
private DBConnection dbConnection;
@InjectMocks
private PostResource postResource;
@Test
public void shouldOpenConnectionBeforeGettingPosts() {
List<Post> posts = postResource.getPosts();
verify(dbConnection).open();
verify(dbConnection).close();
Assert.assertEquals(3, posts.size());
}
}
PostResource
object call methods from mocked DBConnection
class. Mockito is used here to verify if open()
and close()
method were called when calling getPosts()
method on PostResource
object. @InjectMocks annotation creates an instance of a real object, thats why we get 3 posts in the test.
4. Conclusion
In this article, we covered @InjectMocks annotation functionality. Mockito allows injecting mocks by constructor, property, or setter method. Note that to make this annotation work you need to enable it by adding @RunWith(MockitoJUnitRunner.class)
at the top of the unit test class, call MockitoAnnotations.initMocks(this)
method in the @Before JUnit method.
As usual code used in this article is available under GitHub
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}