lundi 10 juillet 2017

How test this service using spring

I am trying to test the restTemplate.exchange but my test always skips the Try

RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
    String uri = resultado.getDatos().getPeticion().getUrl();
    ResponseEntity<ResultadoPeticionDto> respuesta =  null;
    try{
        respuesta = restTemplate.exchange(uri, HttpMethod.GET, entity, ResultadoPeticionDto.class);
        resultado.getDatos().setFechaFin(new Date());
        Calificacion calificacion = null;
        resultado.getDatos().setResultado(calificacion.valueOf(respuesta.getBody().getCodigoError()));
        resultado.getDatos().setMensaje(respuesta.getBody().getMensaje());

    }catch (ResourceAccessException e) {
        resultado.setCodigo(404);
        resultado.setSuccess(false);
        return resultado;
    }catch (Exception e) {
        resultado.setCodigo(500);
        resultado.setSuccess(false);
        return resultado;
    }

this is my Mockito in the test:

Mockito.when(restTemplate.exchange(Mockito.anyString(), 
            Mockito.eq(HttpMethod.GET), Mockito.<HttpEntity<?>> any(), 
            Mockito.eq(ResultadoPeticionDto.class))).thenReturn(response);
    peticionManager.peticion(resultado);

but it doesn't work, in the line of the request it run normally and skip my configuraction in the when, i'm using

  @Mock
private RestTemplate restTemplate;

@InjectMocks
PeticionManager peticionManager;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);

}

in the test class, any idea? The katch captures the exception (code 500) and fails to provide coverage when attempting to be the target of my test

Aucun commentaire:

Enregistrer un commentaire