Search code examples
javaspringjunitjdbctemplate

Mockito.When method not able to return mocked object when the method is called


I see lot of posts on this question but still not able to solve my problem.

when(queryEngineService.getRelationshipWithResources(anyString(), refEq(filterDto), anyString(), anyString(),
                anyInt(), anyInt(), anyString())).thenReturn(tenantResponseDto);
TenantResponseDto dto = queryEngineService.getRelationshipWithResources(anyString(), refEq(filterDto),
                anyString(), anyString(), anyInt(), anyInt(), anyString());

When i debug the object. I'm not able to see the mock data in it. is there anything which I am missing?

Below one is my TestClass:

    public class QueryEngineAPITest extends AbstractTest {

    QueryEngineService queryEngineService = Mockito.mock(QueryEngineService.class);
    TenantResponseDto tenantResponseDto;

    private QueryEngineDto queryEngineDto;
    private RelationshipFilterDto filterDto;
    @Before
    public void setUp() throws Exception {
        super.setUp();
        prepareMockData();

    }

    void prepareMockData() {
        tenantResponseDto = new TenantResponseDto();
        int i = 0;
        queryEngineDto = new QueryEngineDto();
        List<Map<String, Object>> mockDataList = new ArrayList<Map<String, Object>>();
        while (i < 10) {
            Map<String, Object> mockDataMap = new HashMap<String, Object>();
            mockDataMap.put("APPLICATION", "AWS");
            mockDataMap.put("ENVIRONMENT_NAME", "SIGMA LABS" + i);
            mockDataList.add(mockDataMap);
            i++;
        }
        queryEngineDto.setQueryData(mockDataList);
        tenantResponseDto.setResponseAsQueryEngineDto(queryEngineDto);
tenantResponseDto.setResponseCount(Long.valueOf(queryEngineDto.getQueryData().size()));

    filterDto = new RelationshipFilterDto();
    filterDto.setBefore("");
}

@Test
public void getRelationShipsTest() throws Exception {
    when(queryEngineService.getRelationshipWithResources(eq(""), refEq(filterDto), eq(""), eq(""), eq(1), eq(1),
            eq(""))).thenReturn(tenantResponseDto);
    TenantResponseDto dto = queryEngineService.getRelationshipWithResources(eq(""), refEq(filterDto), eq(""),
            eq(""), eq(1), eq(1), eq(""));
    assertFalse(dto.getResponseAsQueryEngineDto().getQueryData().isEmpty());
    assertEquals(Long.valueOf(dto.getResponseAsQueryEngineDto().getQueryData().size()), Long.valueOf(10));

}

Solution

  • Your mocked method with arguments doesn't match method you are calling

    when(queryEngineService.getRelationshipWithResources(eq(""), refEq(filterDto), eq(""), eq(""), eq(1), eq(1),eq(""))).thenReturn(tenantResponseDto);
    TenantResponseDto dto = queryEngineService.getRelationshipWithResources(eq(""), refEq(filterDto), eq(""),
                    eq(""), eq(1), eq(1), eq(""));
    

    You need to change second line to:

    TenantResponseDto dto = queryEngineService.getRelationshipWithResources("",filterDto, "", "", 1, 1, "");
    

    eq and refEq are called argument matchers, when you want to run test you need to provide arguments as they are, not in argument matcher