Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Mockito eq()

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.

// 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 {

@Test
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"));
}
}
Output:

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".

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.


Use the combination of eq() and any(), example:

@Test
void should_NotCompleteBooking_When_PriceTooHigh() {

//Given
double price = 400.0;

BookingRequest br = new BookingRequest("1", LocalDate.of(2020, 01, 01), LocalDate.of(2020,


01, 05), 2, true);

when(psMock.pay(any(BookingRequest.class), eq(price))).thenThrow(BusinessException.class);

/**eq() is the only way to use any() with exact values - any() cannot be combined with raw
values!*/

//When
Executable exe = () -> bs.makeBooking(br);

//Then
assertThrows(BusinessException.class, exe);

You might also like