【问题标题】:mockito and multiple HTTP in Spring Boot testSpring Boot 测试中的 mockito 和多个 HTTP
【发布时间】:2018-04-24 16:24:38
【问题描述】:

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

我必须使用 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 个列表,因此测试无法继续。

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

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

非常感谢您的帮助!

【问题讨论】:

    标签: java testing junit mockito resttemplate


    【解决方案1】:

    我找到了解决方案,而不是使用何时可以拨打一个电话,然后按您需要的顺序多次返回,而不是多次使用密钥,并附上我的工作方式的答案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 2021-05-28
      • 2022-01-27
      • 2020-08-20
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      相关资源
      最近更新 更多