when()
and then()
methodsIn unit testing, mocking objects is a common practice to isolate the code under test and focus solely on the behavior of the unit being tested. Mockito is a popular mocking framework in Java that provides powerful capabilities for creating mock objects and specifying their behavior.
One of the key features of Mockito is the ability to configure the behavior of mock objects using the when()
and then()
methods. These methods allow developers to define the expected behavior of a mock object when a specific method is called.
Let's dive into how we can use these methods to configure the behavior of mock objects effectively:
when()
The when()
method is used to define the expected behavior of a mock object when a specific method is invoked. It takes the method call as an argument and returns an object of type OngoingStubbing<T>
, where T
is the return type of the method being called.
For example, let's say we have a UserService
class with a method getUserCount()
that returns the total number of users. We want to mock this behavior using Mockito:
UserService userService = Mockito.mock(UserService.class);
// Configuring the behavior of the mock object
Mockito.when(userService.getUserCount()).thenReturn(10);
In the above example, we create a mock object of the UserService
class using Mockito.mock()
method. Then, we use the when()
method to define the behavior of the mock object when getUserCount()
method is called. In this case, we specify that the method should return 10
as the result.
then()
The then()
method is used to further customize the behavior of mock objects beyond simple returns. It allows developers to define more complex behaviors, such as throwing exceptions or performing additional actions.
Let's see some examples of customizing behavior using then()
:
Mockito.when(someMock.someMethod()).thenThrow(new RuntimeException("Something went wrong!"));
In this example, we configure the mock object to throw a RuntimeException
with a specific error message when someMethod()
is called.
Mockito.when(someMock.someMethod()).thenAnswer(invocation -> {
AnotherMock anotherMock = Mockito.mock(AnotherMock.class);
Mockito.when(anotherMock.anotherMethod()).thenReturn("Mocked result");
return anotherMock;
});
Here, we configure the mock object to return a dynamically created mock (AnotherMock
) and define its behavior as well. This allows us to create a chain of mock objects and define different behaviors for each method.
Mockito.when(someMock.someMethod()).thenAnswer(invocation -> {
// Perform additional actions
// ...
return someResult;
});
In this case, we can perform additional actions, such as logging, before returning a result from the mock operation.
Mockito's when()
and then()
methods provide convenient ways to configure the behavior of mock objects during unit testing. By using these methods, developers can easily define the expected behavior of mock objects and tailor it to match the specific test requirements. This helps in creating comprehensive unit tests that isolate the code under test and verify its behavior accurately.
noob to master © copyleft