title: Spring MVC Controller Exception Test
body: <p>I have the following code</p>
  
  <pre><code>@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";
  }
  </code></pre>
  
  <p>I'm trying to unit test when NotFoundException is thrown, so i write code like this</p>
  
  <pre><code>@Test(expected = NotFoundException.class)
  public void editFormNotFoundTest() throws Exception{
  
      Mockito.when(categoryService.findOne(1L)).thenReturn(null);
      mockMvc.perform(get("/admin/category/edit/{id}",1L));
  }
  </code></pre>
  
  <p>But failed.
  Any suggestions how to test the exception?</p>
  
  <p>Or should i throw the exception inside CategoryService so i can do something like this</p>
  
  <pre><code>Mockito.when(categoryService.findOne(1L)).thenThrow(new NotFoundException("Message"));
  </code></pre>