简体   繁体   English

Mockito.When 方法无法返回模拟 object 时调用该方法

[英]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.当我调试 object 时。 I'm not able to see the mock data in it.我无法在其中看到模拟数据。 is there anything which I am missing?有什么我想念的吗?

Below one is my TestClass:下面一个是我的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您使用 arguments 的模拟方法与您正在调用的方法不匹配

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 eqrefEq被称为参数匹配器,当你想运行测试时,你需要提供 arguments,而不是在参数匹配器中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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