### Question:
ServletHandler extends XRayHandler { @Override public void handleResponse(Event event) { HttpServletNetworkResponseEvent responseEvent = (HttpServletNetworkResponseEvent) event; Segment currentSegment = getSegment(); if (currentSegment == null) { return; } Map<String, Object> responseAttributes = new HashMap<>(); int statusCode = responseEvent.getStatusCode(); switch (statusCode / 100) { case 2: break; case 4: currentSegment.setError(true); if (statusCode == 429) { currentSegment.setThrottle(true); } break; case 5: currentSegment.setFault(true); break; } responseAttributes.put(STATUS_KEY, statusCode); currentSegment.putHttp(RESPONSE_KEY, responseAttributes); endSegment(); }  @Override void handleRequest(Event event); @Override void handleResponse(Event event);  }### Answer:
@Test public void testContextMissingInResponse() { HttpServletNetworkRequestEvent requestEvent = new HttpServletNetworkRequestEvent(ORIGIN, 1, 1, "test", "test"); HttpServletNetworkResponseEvent responseEvent = new HttpServletNetworkResponseEvent(ORIGIN, requestEvent); AWSXRay.clearTraceEntity(); Assert.assertNull(AWSXRay.getTraceEntity()); servletHandler.handleResponse(responseEvent); }
@Test public void testHandleResponse() { HttpServletNetworkRequestEvent requestEvent = mock(HttpServletNetworkRequestEvent.class); HttpServletNetworkResponseEvent responseEvent = new HttpServletNetworkResponseEvent(ORIGIN, requestEvent); responseEvent.withStatusCode(200); Segment servletSegment = AWSXRay.beginSegment(SERVICE_NAME); servletHandler.handleResponse(responseEvent); Assert.assertEquals(200, ((Map<String, Integer>) servletSegment.getHttp().get("response")).get("status").intValue()); }
@Test public void testHandleResponseErrors() { HttpServletNetworkRequestEvent requestEvent = mock(HttpServletNetworkRequestEvent.class); HttpServletNetworkResponseEvent responseEvent = new HttpServletNetworkResponseEvent(ORIGIN, requestEvent); Segment servletSegment = AWSXRay.beginSegment(SERVICE_NAME); responseEvent.withStatusCode(400); servletHandler.handleResponse(responseEvent); Assert.assertEquals(400, ((Map<String, Integer>) servletSegment.getHttp().get("response")).get("status").intValue()); Assert.assertTrue(servletSegment.isError()); Assert.assertFalse(servletSegment.isFault()); AWSXRay.clearTraceEntity(); servletSegment = AWSXRay.beginSegment(SERVICE_NAME); responseEvent.withStatusCode(500); servletHandler.handleResponse(responseEvent); Assert.assertEquals(500, ((Map<String, Integer>) servletSegment.getHttp().get("response")).get("status").intValue()); Assert.assertFalse(servletSegment.isError()); Assert.assertTrue(servletSegment.isFault()); }