Mockito eq()

1. Overview

In Mockito, the eq() method is an argument matcher used for matching specific values. It allows you to specify expected input values while stubbing methods or verifying invocations. This tutorial provides an in-depth exploration of the eq() method, illustrating its use in various contexts.

2. Development Steps

1. Set up a new Maven project.

2. Add required dependencies: Mockito and JUnit 5.

3. Develop a class to be tested using Mockito.

4. Create a test class to showcase the usage of the eq() method.

5. Execute the test cases.

6. Examine the test outcomes.

3. Dependencies (Mockito and JUnit 5)

<!-- JUnit 5 -->
<!-- Mockito -->

4. Code Program

// Step 3: Establish a BookService class with a method to check availability of a book
class BookService {
    boolean isAvailable(String bookName) {
        // Typically, this would interact with a database or inventory system
        return true;
// Step 4: Construct the test class for BookService
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.eq;
public class BookServiceTest {
    public void isAvailableTest() {
        // Generate a mock of BookService
        BookService mockService = mock(BookService.class);
        // Define the mock's behavior: return true when isAvailable is called with "Mockito Guide"
        when(mockService.isAvailable(eq("Mockito Guide"))).thenReturn(true);
        // Invoke the isAvailable method on the mock
        mockService.isAvailable("Mockito Guide");
        // Validate that the isAvailable method was called with "Mockito Guide" as the argument
        verify(mockService).isAvailable(eq("Mockito Guide"));


The isAvailableTest will succeed, indicating that the mock's isAvailable method was invoked with the argument "Mockito Guide" as anticipated.

Code Explanation:

1. Once Mockito and JUnit 5 dependencies have been incorporated, a rudimentary BookService class is presented. This class comprises an isAvailable method which, under typical conditions, would communicate with a database or inventory.

2. The mock() method in Mockito is employed to create a mock of BookService.

3. The behavior of the mock is set up with the when() method combined with eq("Mockito Guide"), specifying that the isAvailable method should return true when it's called with the argument "Mockito Guide".

4. Subsequently, the isAvailable method is invoked on the mock with the argument "Mockito Guide".

5. With the verify() method combined with eq("Mockito Guide"), it is confirmed that the mock's isAvailable method was triggered with the argument "Mockito Guide".

5. Conclusion

The eq() method in Mockito is a versatile tool, facilitating exact value matching for method arguments. It's beneficial when you want to ensure that a method has been invoked with a specific argument or when you want to stub a method's behavior for certain input values. Employing eq() grants testers the power to verify interactions with precision, making it a fundamental element in the Mockito arsenal.

Related Mockito Methods

Mockito mock()
Mockito spy()
Mockito when()
Mockito thenThrow()
Mockito verify()
Mockito times()
Mockito never()
Mockito any()
Mockito eq()
Mockito inOrder()
Mockito doReturn()
Mockito doThrow()
Mockito doAnswer()
Mockito timeout()
Mockito ArgumentMatchers