BiFunctionWithTracing implements BiFunction<T, U, R> { @Override public R apply(T t, U u) { TracingState originalThreadInfo = null; try { originalThreadInfo = linkTracingToCurrentThread(spanStackForExecution, mdcContextMapForExecution); return origBiFunction.apply(t, u); } finally { unlinkTracingFromCurrentThread(originalThreadInfo); } } BiFunctionWithTracing(BiFunction<T, U, R> origBiFunction);  BiFunctionWithTracing(BiFunction<T, U, R> origBiFunction,
                                 Pair<Deque<Span>, Map<String, String>> originalThreadInfo);  BiFunctionWithTracing(BiFunction<T, U, R> origBiFunction,
                                 Deque<Span> spanStackForExecution,
                                 Map<String, String> mdcContextMapForExecution); static BiFunctionWithTracing<T, U, R> withTracing(BiFunction<T, U, R> origBiFunction); static BiFunctionWithTracing<T, U, R> withTracing(
        BiFunction<T, U, R> origBiFunction,
        Pair<Deque<Span>, Map<String, String>> originalThreadInfo
    ); static BiFunctionWithTracing<T, U, R> withTracing(BiFunction<T, U, R> origBiFunction,
                                                                       Deque<Span> spanStackForExecution,
                                                                       Map<String, String> mdcContextMapForExecution); @Override R apply(T t, U u);  }
@DataProvider(value = { "true", "false" }) @Test public void apply_handles_tracing_and_mdc_info_as_expected(boolean throwException) { throwExceptionDuringCall = throwException; Tracer.getInstance().startRequestWithRootSpan("foo"); Deque<Span> spanStack = Tracer.getInstance().getCurrentSpanStackCopy(); Map<String, String> mdcInfo = MDC.getCopyOfContextMap(); BiFunctionWithTracing instance = new BiFunctionWithTracing( biFunctionMock, spanStack, mdcInfo ); resetTracing(); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isNullOrEmpty(); Throwable ex = null; Object result = null; try { result = instance.apply(inObj1, inObj2); } catch(Throwable t) { ex = t; } verify(biFunctionMock).apply(inObj1, inObj2); if (throwException) { assertThat(ex).isNotNull(); assertThat(result).isNull(); } else { assertThat(ex).isNull(); assertThat(result).isSameAs(outObj); } assertThat(currentSpanStackWhenFunctionWasCalled.get(0)).isEqualTo(spanStack); assertThat(currentMdcInfoWhenFunctionWasCalled.get(0)).isEqualTo(mdcInfo); assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isNull(); assertThat(MDC.getCopyOfContextMap()).isNullOrEmpty(); }