@Test
public void testServletCatchesExceptions() throws IOException, ServletException {

    AWSXRayServletFilter servletFilter = new AWSXRayServletFilter("fail");
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRequestURL()).thenReturn(new StringBuffer("test_url"));
    when(request.getMethod()).thenReturn("TEST_METHOD");
    when(request.isAsyncStarted()).thenReturn(false);
    HttpServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = mock(FilterChain.class);
    Exception ourException = new RuntimeException("Test");
    Mockito.doThrow(ourException).when(chain).doFilter(Mockito.any(), Mockito.any());
    assertThatThrownBy(() -> servletFilter.doFilter(request, response, chain)).isEqualTo(ourException);
    ArgumentCaptor<Segment> emittedSegment = ArgumentCaptor.forClass(Segment.class);
    verify(AWSXRay.getGlobalRecorder().getEmitter(), Mockito.times(1)).sendSegment(emittedSegment.capture());
    Segment segment = emittedSegment.getValue();
    Cause cause = segment.getCause();
    Assert.assertEquals(1, cause.getExceptions().size());
    Throwable storedThrowable = cause.getExceptions().get(0).getThrowable();
    Assert.assertEquals(ourException, storedThrowable);
}