java - Spring Boot 测试中的 mockito 和多个 HTTP

标签 java testing junit mockito resttemplate

我在处理一个项目时遇到了一个大问题,我想知道你是否可以帮助我。

我必须使用 mockito 执行一些单元测试,所有方法都很好!直到您以相同的方法对 http 进行了 2 次调用,而我不知道如何区分它们。

我有以下测试:

  // -----------------------------------------------------------services
@InjectMocks
private SandboxAccountService accountService;

@InjectMocks
private SandboxBalancesService balancesService;

@InjectMocks
private SandboxMovementsService movementService;


@Mock
private RestTemplate restTemplate;

@Mock
private RestTemplate restTemplateMovimientos;


@Test
public void test_movementsServiceImpl() throws Exception {



    //LLAMADA A LISTA DE Account

    List<Account> accountList = new ArrayList<>();
    accountList.add(account);
    accountList.add(account2);

    ResponseEntity<List<Account>> list = new ResponseEntity<List<Account>>(accountList, HttpStatus.OK);


    // FIRST HTTP CALL
    when(restTemplate.exchange(anyString() , any(HttpMethod.class),
            any(HttpEntity.class), any(ParameterizedTypeReference.class))).thenReturn(list);

    //LLAMADA A LISTA DE MOVIMIENTOS

    listMovent.add(movement);
    listMovent.add(movementDos);

    ResponseEntity<List<Movement>> listaMovi = new ResponseEntity<List<Movement>>(listMovent, HttpStatus.OK);

   // Second HTTP CALL
    when(restTemplateMovimientos.exchange(anyString() , any(HttpMethod.class),
            any(HttpEntity.class), any(ParameterizedTypeReference.class))).thenReturn(listaMovi);






try {
    AccountsMovementsResponse accountsMovementsResponse = movementService.getMovements(accountsMovementsRequest,
            AUTORIZATHION_TOKEN, language);
} catch (Exception e) {

}

}

当调试正确地为我列出列表时,一切都很好,但是当他切换到服务时

    //// This its a primary http ( Account)
    ResponseEntity<List<Account>> exchange = restTemplate.exchange(sandboxAccountURL + userId, HttpMethod.GET,entity,
            new ParameterizedTypeReference<List<Account>>() {
            });

    // This list its Account CORRECT
    List<Account> lista=exchange.getBody();


   // code.....

    // This its a second http ( movement )
    ResponseEntity<List<Movement>> movementList = restTemplate.exchange(GenerateUrl, HttpMethod.GET,entity,
                            new ParameterizedTypeReference<List<Movement>>() {
                            });
                 // This list should be moves, but it's a list of accounts.
                 List<Movement> listMovement= movementList.getBody();

我的大问题是,我没有 2 个不同的列表,而是有 2 个列表,所以测试无法继续。

如果我尝试代码,一切正常,并使其正常工作,我遇到的问题是,在测试时它会克隆列表。

我不知道是否有办法让 mock 的“when”可以使它们不同,因为它让我明白当我这样做时它需要第一个。

非常感谢您的帮助!

最佳答案

我找到了解决方案,而不是多次使用 key ,而是使用何时可以进行一次调用,然后按照您需要的顺序多次返回并附上我如何工作的答案

when(restTemplate.exchange(anyString(), any(HttpMethod.class),
            any(HttpEntity.class), any(ParameterizedTypeReference.class))).thenReturn(list).thenReturn(listaMovi);

关于java - Spring Boot 测试中的 mockito 和多个 HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50006609/

相关文章:

Java数字签名和C++ CryptSignMessage导致的结果不同

java - SonarQube 不显示单元测试结果

java - 尽管出现错误,如何继续循环中的测试

Java比较数组

java - 设备未准备好 gradle 同步失败

java - 如何在测试范围内使用 m2e (Eclipse/Maven) 运行具有测试依赖项的命令行程序?

javascript - js switch语句单元测试

java - 从 Json 路径查询返回并在 Junit 断言中使用的字符串的排序

java - 使用 getResourceAsStream() 到项目文件夹外部的文件的绝对路径

unit-testing - 如何避免重复测试代码