Difference Between ‘Mockito.when’ and ‘when’ in Mockito
When writing unit tests with Mockito, you may come across different ways to set up mock behaviors. This article will explore the differences between using 'Mockito.when‘ and ‘when' in your test code. Although both methods serve the same purpose, their usage can differ based on context and preference.
Introduction to Mockito
Mockito is a popular mocking framework for Java that allows you to create mock objects for testing purposes. By simulating the behavior of real objects, Mockito enables you to write unit tests that are isolated from the actual implementation.
The 'Mockito.when‘ Method
The 'Mockito.when' method is a way to specify the behavior of a mock object when a particular method is called. Here’s an example:
Mockito.when(questionAndAnswerRepository.findById(quesAndAnsExtDTO.getId())).thenReturn(Optional.empty());
The 'when‘ Method with Static Import
The ‘when‘ method can also be used with a static import. If you have a static import for ‘Mockito.when‘, it looks like this:
import static org.mockito.Mockito.when;
You can write your mock setup more concisely:
when(questionAndAnswerRepository.findById(quesAndAnsExtDTO.getId())).thenReturn(Optional.empty());
Detailed Explanation
Static Import
- The
'when'method is a static method from theMockitoclass. - Using a static import allows you to call
'when‘ directly without theMockitoprefix.
Functionality
- Both
'Mockito.when‘ and ‘when'achieve the same functionality: they set up a mock behavior such that whenquestionAndAnswerRepository.findById(quesAndAnsExtDTO.getId())is called, it returnsOptional.empty().
Readability and Preference
- Using
'Mockito.when‘ can sometimes make it clearer to someone reading the code that this method is coming from theMockitolibrary, especially if static imports are not commonly used in your codebase. - Using
'when‘ with a static import can make the code cleaner and shorter. This is often a matter of personal or team preference.
Example Usage in a Test Class
Here’s how you might see both methods used in context:
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
import org.mockito.Mockito;import org.junit.jupiter.api.Test;
public class ExampleTest {
@Test
public void exampleTest() {
// Using Mockito.when directly
QuestionAndAnswerRepository questionAndAnswerRepository = mock(QuestionAndAnswerRepository.class);
QuesAndAnsExtDTO quesAndAnsExtDTO = new QuesAndAnsExtDTO();
quesAndAnsExtDTO.setId(1L); Mockito.when(questionAndAnswerRepository.findById(quesAndAnsExtDTO.getId())).thenReturn(Optional.empty());
// Using when with a static import when(questionAndAnswerRepository.findById(quesAndAnsExtDTO.getId())).thenReturn(Optional.empty()); }
}
In summary, there is no functional difference between ‘Mockito.when‘ and ‘when‘ in the context of setting up mock behavior with Mockito. The choice between them depends on whether you prefer to use static imports for brevity or fully qualified method calls for clarity.