1

New to Junit, Please help me find mockito matchers for Optional and Map<String, String[]>.

public Model<SummaryModel> getAll(Map<String, String[]> queryParameters, Integer page,
            Integer limit, Optional<String> sort) throws PageNumberOutOfBoundsException;

when(service.getAll( any(), anyInt(),anyInt(), any())).thenReturn(mockedResource);
pm1090
  • 101
  • 1
  • 10

1 Answers1

1

We have different options, based on our requirement we can use the same. Below is some example of Optional and Map<String, String[]>.

For Map<String, String[]>

  1. You can create your actual map with values and then pass like Mockito.eq(myMap);
  2. You can use Mockito.anyMap()

For Optional

  1. Mockito.eq(Optional.ofNullable(sort))
  2. Mockito.any(Optional.class)
SSK
  • 3,444
  • 6
  • 32
  • 59
  • 1
    For the case of `Optional`, you can pass `eq(Optional.ofNullable(anyObject()))`. Where both `eq` and `anyObject` have to be statically imported from `org.mockito.Matchers`. – Nicolás Ozimica May 18 '22 at 01:00