mardi 24 avril 2018

mockito and multiple HTTP in Spring Boot test

I'm having a big problem with a project that I'm working on and I'm wondering if you could help me.

I have to perform a few unit tests using mockito, all methods go great! until you have 2 calls to http in the same method and I don't know how to differentiate them.

I have the following on the tests:

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

    Account account = new Account();
    account.setAlias("");
    account.setBalance("3000");
    account.setCreditDebitIndicator("CRDT");
    account.setIban("ES3800490075472016058519");

    Account account2 = new Account();
    account2.setAlias("");
    account2.setBalance("3000");
    account2.setCreditDebitIndicator("CRDT");
    account2.setIban("ES7500490001522415794234");

    //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
    List<Movement> listMovent = new ArrayList<>();
    Movement movement = new Movement();
    movement.setAmount("10");
    movement.setBalance("+2485.58");
    movement.setCurrency("EUR");
    movement.setDescription("COMPRA EN CINES MAD, MADRID, TARJ. :*050502");
    movement.setTransactionDate("2018-04-20");
    movement.setValueDate("2018-04-20");

    Movement movementDos = new Movement();
    movementDos.setAmount("-10");
    movementDos.setBalance("+2485.58");
    movementDos.setCurrency("EUR");
    movementDos.setDescription("COMPRA EN CINES MAD, MADRID, TARJ. :*050502");
    movementDos.setTransactionDate("2018-04-20");
    movementDos.setValueDate("2018-04-20");

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

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

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



AccountsMovementsRequest accountsMovementsRequest = new AccountsMovementsRequest();
String language = "es-ES";

App app = new App();

app.setClientId("48415b34-650c-4f3a-bb19-3356ea406b99");
app.setClientName("Idealista S.A");
app.setOrgId("59c89bfe4b0baa20c30a072");
app.setOrgName("Idealista Comunicaciones");

accountsMovementsRequest.setApp(app);
accountsMovementsRequest.setState("movementsForUserxi123");
accountsMovementsRequest.setOrder("ASC");

BigDecimal movementAmount = new BigDecimal("10");
accountsMovementsRequest.setAmount_from(movementAmount);
BigDecimal movementAmountTo = new BigDecimal("23");
accountsMovementsRequest.setAmount_to(movementAmountTo);
accountsMovementsRequest.setDate_from("2018-04-20");
accountsMovementsRequest.setDate_to("2018-04-24");
accountsMovementsRequest.setMovement("IN");

List<String> listaCuentas = new ArrayList<String>();
listaCuentas.add("ES3800490075472016058519");
listaCuentas.add("ES7900490001502615793904");
listaCuentas.add("ES6000494099422594091531");

accountsMovementsRequest.setAccounts(listaCuentas);

accountsMovementsRequest.setPsu_active("1");
accountsMovementsRequest.setOperation("movements");




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

}

}

When debug does the lists for me correctly and all well but when he switches to the service

HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
    headers.putAll(Utils.createHeadersBase64(sandboxU, sandboxP));
    HttpEntity<?> entity = new HttpEntity<>( headers);

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

My big problem is that instead of having 2 different lists I have 2 lists of them so the test can't continue.

if i try the code everything works without a problem and makes it work, the problem i have is that at the moment of testing it clones the lists.

I don't know if there is a way to make the mock's "when" can make them different because it makes me understand that it takes the first when I do.

Thank you very much for your help!

Aucun commentaire:

Enregistrer un commentaire