Eu fiz um controller para chamar um categoria:
@GetMapping(value = "/{id}")
public Categoria findById(@PathVariable Integer id) {
log.info("[GET] Obtendo Categoria por id: ID = " + id);
return this.categoriaService.findById(id);
}
No service tem o tratamento de exceção:
@Override
public Categoria findById(Integer id) {
return this.categoriaRepository.findById(id)
.orElseThrow(() -> new ObjectNotFoundException("Objeto não encontrado! Id: " + id));
}
Quando chamo pelo `Postman retorna a exceção corretamente mas no meu teste ele não retorna, quando fui debugar vi que está chamado o MockMethodInterceptor no controller.
@Test
@DisplayName("Deve buscar uma categoria que não existe e retornar o status not_found")
public void deveBuscarUmaCategoriaQueNaoExistente() throws Exception {
Integer id = 1;
String msg = "Objeto não encontrado! Id: " + id;
MockHttpServletRequestBuilder request = MockMvcRequestBuilders
.get(CATEGORIA_API.concat("/"+id))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON);
this.mvc
.perform(request)
.andExpect(MockMvcResultMatchers.status().isNotFound())
.andExpect(MockMvcResultMatchers.jsonPath("status").value("404"))
.andExpect(MockMvcResultMatchers.jsonPath("msg").value(msg));
}
Minha classe de service é CategoriaServiceImpl e ela implementa a CategoriaService.