javaspring-bootmockito

Mockito giving error Strict stubbing argument mismatch


Need Help...

I tried to run mockito unit test but it giving error`Strict stubbing argument mismatch. does anyone know why this error happened is there something wrong with my code ?

Any response would be highly appreciated.

Please check:

this invocation of 'getCoopPipelineItems' method: aggregateService.getCoopPipelineItems( [0], "submitted", "filter", Mock for Pageable, hashCode: 2037183157 ); -> at io.portfolioaggregate.service.rest.PortfolioAggregateRestController.getCoopPipelineOutlinesWithFilter(PortfolioAggregateRestController.java:92)

has following stubbing(s) with different arguments:

aggregateService.getCoopPipelineItems( [0], "", "", null );

Here's my code:

@Test
  public void getCoopPipelineOutlinesWithFilter_returnsExpectedResult() {
    PagedResult<PipelineItem> expected = mock(PagedResult.class);
    when(aggregateService.getCoopPipelineItems(List.of(anyInt()), anyString(), anyString(), any(Pageable.class)))
        .thenReturn(expected);


    List<Integer> orgIds = List.of(0);
    PagedResult<PipelineItem> result = controller.getCoopPipelineOutlinesWithFilter(orgIds, "submitted", "filter", mock(Pageable.class));

    Assertions.assertEquals(expected, result);
    verify(aggregateService).getCoopPipelineItems(orgIds, "submitted", "filter",
        mock(Pageable.class));
  }

fix the error of my code and unit test pass


Solution

  • Method getCoopPipelineItems itself takes a java.util.List as the first parameter. But the list you are passing in during mock invocation is not a mock itself but an actual list whose element is the return value of the invocation of anyInt(). So when trying to set up the stub mockito sees the actual arguments of type java.util.List, java.lang.String, java.lang.String, null as Pageable. But from the argument matchers mockito observes int, String, String, Pageable. The mismatch is java.util.List against int.

    You should use anyList() for the first parameter of getCoopPipelineItems and add verifcations using ArgumentCaptor to check that the invocation actually had a single Integer contained in the list. Also note that a List can only contain Integer and not int since the latter is a primitive type.