次のコードがあります
@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";
}
NotFoundExceptionがスローされたときに単体テストをしようとしているので、このようなコードを書きます
@Test(expected = NotFoundException.class)
public void editFormNotFoundTest() throws Exception{
Mockito.when(categoryService.findOne(1L)).thenReturn(null);
mockMvc.perform(get("/admin/category/edit/{id}",1L));
}
しかし、失敗しました。例外をテストする方法について何か提案はありますか?
または、CategoryService 内で例外をスローして、このようなことができるようにする必要があります
Mockito.when(categoryService.findOne(1L)).thenThrow(new NotFoundException("Message"));