Mockito: What Is Mocking?
Mockito: What Is Mocking?
Mockito is a mocking framework, JAVA-based library that is used for effective unit
testing of JAVA applications. Mockito is used to mock interfaces so that a dummy
functionality can be added to a mock interface that can be used in unit testing.
What is Mocking?
Mocking is a way to test the functionality of a class in isolation. Mocking does not
require a database connection or properties file read or file server read to test
a functionality. Mock objects do the mocking of the real service. A mock object
returns a dummy data corresponding to some dummy input passed to it.
Mockito
Mockito facilitates creating mock objects seamlessly. It uses Java Reflection in order
to create mock objects for a given interface. Mock objects are nothing but proxy for
actual implementations.
Consider a case of Stock Service which returns the price details of a stock. During
development, the actual stock service cannot be used to get real-time data. So we
need a dummy implementation of the stock service. Mockito can do the same very
easily, as its name suggests.
Setup Environment
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.7.7</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId> // it is meant for junit 5
<version>3.7.7</version>
<scope>test</scope>
</dependency>
If you use the @Mock annotation, you must trigger the initialization of the annotated fields.
The MockitoExtension does this by calling the static
method MockitoAnnotations.initMocks(this).
Using when().thenReturn() and when().thenThrow()
Mocks can return different values depending on arguments passed into a method.
The when(….).thenReturn(….) method chain is used to specify a return value for a
method call with pre-defined parameters.
You also can use methods like anyString or anyInt to define that dependent on the input
type a certain value should be returned.
If you specify more than one value, they are returned in the order of specification, until the last
one is used. Afterwards the last specified value is returned.
Mockito Annotations
The Mockito framework provides a variety of annotations to make the code simple
and easy to understand. Also, it reduces the lines of code that helps in focusing on
the business logic. In Mockito, annotations are useful when we want to use the
mocked object at different places to avoid calling the same methods multiple times.
o @Mock: It is used to mock the objects that helps in minimizing the repetitive
mock objects. It makes the test code and verification error easier to read as
parameter names (field names) are used to identify the mocks. The @Mock
annotation is available in the org.mockito package.
o @RunWith: It is a class-level annotation. It is used to keep the test clean
and improves debugging. It also detects the unused stubs available in the
test and initialize mocks annotated with @Mock annotation. The @RunWith
annotation is available in the org.mockito.junit package.
@RunWith(MockitoJUnitRunner.class)
public class ToDoBusinessMock {
@Mock
ToDoService servicemock;
}
Note: The @Mock annotation is always used with the @RunWith annotation.
@InjectMocks
ToDoBusiness business;