Tôi có đoạn code sauSpring MVC điều khiển ngoại lệ Kiểm tra
@RequestMapping(value = "admin/category/edit/{id}",method = RequestMethod.GET)
public String editForm(Model model,@PathVariable Long id) throws NotFoundException{
Category category=categoryService.findOne(id);
if(category==null){
throw new NotFoundException();
}
model.addAttribute("category", category);
return "edit";
}
Tôi đang cố gắng để kiểm tra đơn vị khi NotFoundException được ném ra, vì vậy tôi viết mã như thế này
@Test(expected = NotFoundException.class)
public void editFormNotFoundTest() throws Exception{
Mockito.when(categoryService.findOne(1L)).thenReturn(null);
mockMvc.perform(get("/admin/category/edit/{id}",1L));
}
Nhưng thất bại. Bất kỳ đề xuất nào về cách kiểm tra ngoại lệ?
Hoặc tôi nên ném ngoại lệ bên trong CategoryService vì vậy tôi có thể làm một cái gì đó như thế này
Mockito.when(categoryService.findOne(1L)).thenThrow(new NotFoundException("Message"));
các getSimpleMappingExceptionResolver() thực hiện là gì? –
Tôi đã phát hiện ra điều này: http://www.mytechnotes.biz/2012/11/spring-mvc-error-handling.html. Có vẻ như org.springframework.web.servlet.handler.SimpleMappingExceptionResolver đã là lớp Spring – josete