title: how to treat controller exception with mockmvc
body: <p>I am using MockMVC to test my controller. </p>
  
  <p>I have the following controller:</p>
  
  <pre><code>public class A{
  
      ...
  
      @RequestMapping("/get")
      public List&lt;ADTO&gt; get(@RequestParam(defaultValue = "15", required = false) Integer limit) throws IOException {
          if(limit &lt;= 0 || limit &gt;= 50){
              throw new IllegalArgumentException();
          }
          ...
          return aDTOs;
      }
  
  }
  </code></pre>
  
  <p>And my current test looks like this:</p>
  
  <pre><code>@Test
  public void testGetAllLimit0() throws Exception {
      mockMvc.perform(get("/A/get")
              .param("limit","0")
              )
              .andDo(print())
              .andExpect(...);
  }
  </code></pre>
  
  <p>I am instantiating MockMVC with this:</p>
  
  <pre><code>@Before
  public void setup() {
      this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
  }
  </code></pre>
  
  <p>How can I take care of that exception thrown in my controller?</p>
  
  <p>Later Edit:</p>
  
  <p>I'm not sure what happened in my code recently but it passes the test:</p>
  
  <pre><code>@Test
  public void testGetAllLimit0() throws Exception {
      mockMvc.perform(get("/A/get")
              .param("limit","0")
              )
              .andDo(print())
              .andExpect(status().is(500));
  }
  </code></pre>
  
  <p>It still passes if I replace <code>is(500)</code> with <code>isOk()</code>. And this is not good, I should check somehow for that exception.</p>
  
  <p>If I run a <code>gradle build</code> I get this:</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException
  </code></pre>