Spring Boot and Mockito

0

I am trying to solve a problem that I have in spring boot and unit tests with mockito.

In my test I am making 2 mocketed http calls to deal with the answers

 // -----------------------------------------------------------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) {

   }

   }

With this each call has its list and when debugging the test works perfectly, but when I go to the service.

  //Esta llamada http contiene la lista mockeada de cuentas correctamente
ResponseEntity<List<Account>> exchange = restTemplate.exchange(sandboxAccountURL + userId, HttpMethod.GET,entity,
            new ParameterizedTypeReference<List<Account>>() {
            });


  //Pero esta llamada no tiene movimientos, vuelve a repetir cuentas


ResponseEntity<List<Movement>> movementList = restTemplate.exchange(GenerateUrl, HttpMethod.GET,entity,
                            new ParameterizedTypeReference<List<Movement>>() {
                            });

I have tried everything, although my knowledge with mockito is basic, if there is any way to differentiate the routes?

    
asked by AntoCode 25.04.2018 в 09:39
source

1 answer

0

I found the answer, instead of making several calls from the (when) only send one and then pass the return that required according to the number of times the call has been requested annex the code response

 when(restTemplate.exchange(anyString(), any(HttpMethod.class),
            any(HttpEntity.class),any(ParameterizedTypeReference.class))).thenReturn(list).thenReturn(listaMovi);
    
answered by 25.04.2018 / 10:10
source