简体   繁体   中英

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));

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM