### Question:
BaseAbstractXRayInterceptor { protected Object processXRayTrace(ProceedingJoinPoint pjp) throws Throwable { try { Subsegment subsegment = AWSXRay.beginSubsegment(pjp.getSignature().getName()); if (subsegment != null) { subsegment.setMetadata(generateMetadata(pjp, subsegment)); } return XRayInterceptorUtils.conditionalProceed(pjp); } catch (Exception e) { AWSXRay.getCurrentSegmentOptional().ifPresent(x -> x.addException(e)); throw e; } finally { logger.trace("Ending Subsegment"); AWSXRay.endSubsegment(); } }  @Around("xrayTracedClasses() || xrayEnabledClasses()") Object traceAroundMethods(ProceedingJoinPoint pjp);  }### Answer:
@Test public void testNullSegmentOnProcessFailure() throws Throwable { Exception expectedException = new RuntimeException("An intended exception"); when(mockRecorder.beginSubsegment(any())).thenThrow(expectedException); when(mockRecorder.getCurrentSegmentOptional()).thenReturn(Optional.empty()); when(mockRecorder.getCurrentSegment()).thenReturn(null); try { xRayInterceptor.processXRayTrace(mockPjp); } catch (Exception e) { assertNotEquals(NullPointerException.class, e.getClass()); assertEquals(expectedException.getMessage(), e.getMessage()); } }
@Test public void testSegmentOnProcessFailure() throws Throwable { Exception expectedException = new RuntimeException("An intended exception"); when(mockRecorder.beginSubsegment(any())).thenThrow(expectedException); Segment mockSegment = mock(Segment.class); when(mockRecorder.getCurrentSegmentOptional()).thenReturn(Optional.of(mockSegment)); when(mockRecorder.getCurrentSegment()).thenReturn(mockSegment); try { xRayInterceptor.processXRayTrace(mockPjp); } catch (Exception e) { verify(mockSegment).addException(expectedException); } }