title: Write JUnit test for @ExceptionHandler
body: <p>I am writing a Rest service using Spring MVC. Here is the outline of the class:</p>
  
  <pre><code> @Controller
   public class MyController{
  
       @RequestMapping(..)
       public void myMethod(...) throws NotAuthorizedException{...}
  
       @ExceptionHandler(NotAuthorizedException.class)
       @ResponseStatus(value=HttpStatus.UNAUTHORIZED, reason="blah")
       public void handler(...){...}
   }
  </code></pre>
  
  <p>I have written my unit tests using the design posted <a href="https://stackoverflow.com/a/9140291/905762">here</a>. The test is basically as follows:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(....)
  public class mytest{
  
      MockHttpServletRequest requestMock;
      MockHttpServletResponse responseMock;
      AnnotationMethodHandlerAdapter handlerAdapter;
  
  @Before
  public void setUp() {
      requestMock = new MockHttpServletRequest();
      requestMock.setContentType(MediaType.APPLICATION_JSON_VALUE);
      requestMock.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
  
      responseMock = new MockHttpServletResponse();
  
      handlerAdapter = new AnnotationMethodHandlerAdapter();
  }
  
  @Test
  public void testExceptionHandler(){
      // setup ....
      handlerAdapter.handle(...);
  
      // verify
      // I would like to do the following
      assertThat(responseMock.getStatus(), is(HttpStatus.UNAUTHORIZED.value()));
  }
  
  }
  </code></pre>
  
  <p>However, the call to <code>handle</code> is throwing the <code>NotAuthorizedException</code>. I have read that this is by design to be able to unit test that the method throws the appropriate exception, however I would like to write an automated test that the framework is handling this exception appropriately and that the class under test has implemented the handler appropriately. Is there a way to do this?</p>
  
  <p>Please be aware that I do not have access to the actual code in a place where I could post it.</p>
  
  <p>Also, I am limited (for unfortunate reasons) to Spring 3.0.5 or 3.1.2.</p>