@Test
public void testNullSegmentOnProcessFailure() throws Throwable {

    // Test to ensure that getCurrentSegment()/getCurrentSegmentOptional() don't throw NPEs when customers are using
    // the Log context missing strategy during the processXRayTrace call.
    Exception expectedException = new RuntimeException("An intended exception");
    // Cause an intended exception to be thrown so that getCurrentSegmentOptional() is called.
    when(mockRecorder.beginSubsegment(any())).thenThrow(expectedException);
    when(mockRecorder.getCurrentSegmentOptional()).thenReturn(Optional.empty());
    when(mockRecorder.getCurrentSegment()).thenReturn(null);
    try {
        xRayInterceptor.processXRayTrace(mockPjp);
    } catch (Exception e) {
        // A null pointer exception here could potentially indicate that a call to getCurrentSegment() is returning null.
        // i.e. there is another exception other than our intended exception that is thrown that's not supposed to be thrown.
        assertNotEquals(NullPointerException.class, e.getClass());
        assertEquals(expectedException.getMessage(), e.getMessage());
    }
}