title: Mockito Expect an Exception
body: <p>I'm trying to test the method below with Mockito and Junit:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>@Transactional
  @RequestMapping(method=RequestMethod.PUT,value ="/updateEmployer/{empId}")
  public @ResponseBody Object updateEmployer(@PathVariable Integer empId,) throws Exception {
  
      Employee e = EmployeeRepository.findOne(empId);
  
      for (Department de : e.getDepartement()){
          de.setDepartmentName(e.getName + "_" + de.getName());       
      }
      EmployeeRepository..saveAndFlush(e);
      return null;
  }   
  </code></pre>
  
  <p>This is the method Test:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Test  // throw java.lang.NullPointerException
   public void updateEmployeeFailureTest() throws Exception {
  
          mockMvc.perform(
              MockMvcRequestBuilders
                      .put("/updateEmployer/{empId}",18)                      
                      .accept(MediaType.APPLICATION_JSON)).andDo(print())         
  
                .andExpect(MockMvcResultMatchers.view().name("errorPage"))
                 .andExpect(MockMvcResultMatchers.model().attributeExists("exception"))
                .andExpect(MockMvcResultMatchers.forwardedUrl("/WEB-INF/jsp/errorPage.jsp"))
                .andExpect(MockMvcResultMatchers.status().isInternalServerError());       
  
      }   
  </code></pre>
  
  <p>The printstack:</p>
  
  <pre class="lang-java prettyprint-override"><code> MockHttpServletRequest:
       HTTP Method = PUT
       Request URI = /updateEmployer/18
        Parameters = {}
           Headers = {Content-Type=[application/json], Accept=   application/json]}
  
           Handler:
              Type = com.controllers.employeeController
            Method = public java.lang.Object    com.controllers.employeeController.updateEmployer(java.lang.Integer) throws   java.lang.Exception
  
             Async:
        Was async started = false
        Async result = null
  
      Resolved Exception:
              ***Type = java.lang.NullPointerException***
  
      ModelAndView:
         View name = errorPage
              View = null
         Attribute = exception
             ***value = java.lang.NullPointerException***
  
           FlashMap:
  
    MockHttpServletResponse:
            Status = 500
     Error message = null
           Headers = {}
      Content type = null
              Body = 
     Forwarded URL = /WEB-INF/jsp/errorPage.jsp
    Redirected URL = null
           Cookies = []
  </code></pre>
  
  <p>It's work but when i try to catch the text or the exception throwed  by this method<br>
  adding  @Test (expected= java.lang.NullPointerException.class)  i have this error:</p>
  
  <p><strong><em>java.lang.AssertionError: Expected exception: java.lang.NullPointerException</em></strong>  </p>
  
  <p>when i try to get the nullPointerException Text as a value of the attribute (exception) of the section  ModelAndView i get this error:</p>
  
  <p><strong><em>java.lang.AssertionError: Model attribute 'exception' expected:java.lang.NullPointerException but  was:java.lang.NullPointerException</em></strong></p>
  
  <p>Is there a way to expect the exception throwed or the text in the value attribut ( value = java.lang.NullPointerException) or the Text in the Resolved Exception section using mockito (mockmvc)?</p>
  
  <p>Any help will be much appreciated</p>