Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you provide examples of using Mockito argument matchers with collections


Can you provide examples of using Mockito argument matchers with collections


Mockito argument matchers are powerful tools for testing with mock objects, allowing you to verify or stub methods with specific or generic arguments. When working with collections, you can use various matchers to ensure that your tests are robust and flexible. Here are some examples of using Mockito argument matchers with collections:

Using `anyCollection()`

If you want to verify that a method is called with any collection, regardless of its contents, you can use `anyCollection()`. This is useful when you don't care about the specific elements in the collection.

java
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;

// Example interface
public interface Service {
    void perform(Collection elements);
}

// Example test
@Test
public void testAnyCollection() {
    Service service = Mockito.mock(Service.class);
    
    // Perform business logic that calls service.perform()
    
    Mockito.verify(service).perform(ArgumentMatchers.anyCollection());
}

Using `eq()` with a Specific Collection

If you need to verify that a method is called with a specific collection, you can use `eq()` along with a predefined collection. However, this requires the collection to have a proper `equals()` implementation, which might not be the case for all collections.

java
@Test
public void testSpecificCollection() {
    Service service = Mockito.mock(Service.class);
    Collection expectedCollection = Arrays.asList("a", "b");
    
    // Perform business logic that calls service.perform()
    
    Mockito.verify(service).perform(ArgumentMatchers.eq(expectedCollection));
}

Using `argThat()` with a Custom Matcher

For more complex scenarios, such as verifying the contents of a collection in a specific order, you can use `argThat()` with a custom matcher. This approach allows you to define a custom condition for the collection.

java
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

// Custom matcher for a collection containing "a" and "b" in order
Matcher> containsInOrder = Matchers.contains("a", "b");

@Test
public void testCollectionInOrder() {
    Service service = Mockito.mock(Service.class);
    
    // Perform business logic that calls service.perform()
    
    Mockito.verify(service).perform(ArgumentMatchers.argThat(containsInOrder));
}

However, as noted in the search results, using `contains()` with `argThat()` might not work directly due to generics issues. In such cases, using an `ArgumentCaptor` is recommended.

Using `ArgumentCaptor`

When the above methods are not suitable, especially when dealing with mutable collections or needing to inspect the collection's state at the time of method invocation, using an `ArgumentCaptor` is a good alternative.

java
import org.mockito.ArgumentCaptor;

@Test
public void testWithArgumentCaptor() {
    Service service = Mockito.mock(Service.class);
    ArgumentCaptor> captor = ArgumentCaptor.forClass(Collection.class);
    
    // Perform business logic that calls service.perform()
    
    Mockito.verify(service).perform(captor.capture());
    
    Collection capturedCollection = captor.getValue();
    // Assert the contents of capturedCollection as needed
}

Conclusion

Mockito argument matchers provide a flexible way to test methods that involve collections. By choosing the right matcher or approach based on your testing needs, you can ensure that your tests are both robust and maintainable.

Citations:
[1] https://stackoverflow.com/questions/20441594/mockito-and-hamcrest-how-to-verify-invocation-of-collection-argument
[2] https://groups.google.com/g/mockito/c/_A4BpsEAY9s
[3] https://mockk.io
[4] https://www.digitalocean.com/community/tutorials/mockito-argument-matchers-any-eq
[5] https://www.digitalocean.com/community/tutorials/mockito-verify
[6] https://stackoverflow.com/questions/16458136/mockito-invalid-use-of-argument-matchers
[7] https://www.softwaretestinghelp.com/mockito-matchers/
[8] http://www.bennorthrop.com/Snippets/verfiying-parameters-of-mocks-with-mockito.php
[9] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.ArgumentMatchers.anyCollection