title: Spring unit test 404\unknown url in mock mvc
body: <p>I would like to test that when an unknown url is requested and a 404 error is generated that my web app actually redirects to the right place.</p>
  
  <p>I havent been able to get this working, I think because tomcat is handling the 404 errors so the forwardedUrl is always null for the tests. I know this works in reality because if I enter some rubbish into the url my app does redirect to my custom page.</p>
  
  <p>My unit test looks like:</p>
  
  <pre><code>@Test
  public void testUnknownUrl() throws Exception {
  mockMvc.perform(get("/url_doesnt_exist"))
      .andExpect(status().isNotFound())        
      .andExpect(forwardedUrl("/static/error/Sorry.html"));             
  }
  </code></pre>
  
  <p>My web.xml configuration is:</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;error-page&gt;
      &lt;error-code&gt;404&lt;/error-code&gt;
      &lt;location&gt;/static/error/Sorry.html&lt;/location&gt;
  &lt;/error-page&gt;
  </code></pre>
  
  <p>The mapping for /static is defined in my spring config like:</p>
  
  <pre><code>&lt;resources mapping="/static/**" location="/resources/" /&gt;
  </code></pre>
  
  <p>Ultimately I would like to mock a request to an unknown url and then check that the page being returned is /static/error/Sorry.html.</p>
  
  <p>Am I doing something wrong or is this not the way to handle 404 etc in spring? The check of the forwarded url in the unit test is always null.</p>
  
  <p>A slightly different question but related all the same is, at what point does the tomcat error handling get invoked over and above the spring controller advice handling?</p>