### Question:
TracingQueryGateway implements QueryGateway { @Override public <R, Q> CompletableFuture<R> query(String queryName, Q query, ResponseType<R> responseType) { Message<?> queryMessage = GenericMessage.asMessage(query); return getWithSpan( "query_" + SpanUtils.messageName(query.getClass(), queryName), queryMessage, queryName, (childSpan) -> delegate.query(queryName, queryMessage, responseType) .whenComplete((r, e) -> { childSpan.log("resultReceived"); childSpan.finish(); }) ); } protected  TracingQueryGateway(Builder builder); static Builder builder(); @Override CompletableFuture<R> query(String queryName, Q query, ResponseType<R> responseType); @Override Stream<R> scatterGather(String queryName,
                                          Q query,
                                          ResponseType<R> responseType,
                                          long timeout,
                                          TimeUnit timeUnit); @Override SubscriptionQueryResult<I, U> subscriptionQuery(String queryName,
                                                                     Q query,
                                                                     ResponseType<I> initialResponseType,
                                                                     ResponseType<U> updateResponseType,
                                                                     SubscriptionQueryBackpressure backpressure,
                                                                     int updateBufferSize); @Override Registration registerDispatchInterceptor(
            MessageDispatchInterceptor<? super QueryMessage<?, ?>> dispatchInterceptor);  }### Answer:
@Test void testQuery_queryName() throws ExecutionException, InterruptedException { when(mockQueryBus.query(any(QueryMessage.class))) .thenReturn(CompletableFuture.completedFuture(answer1)); MockSpan span = mockTracer.buildSpan("testQuery_queryName").start(); ScopeManager scopeManager = mockTracer.scopeManager(); try (final Scope ignored = scopeManager.activate(span)) { CompletableFuture<String> query = testSubject.query("pointQuery", "Query", String.class); assertEquals("answer1", query.get()); Span activeSpan = mockTracer.activeSpan(); assertEquals(span, activeSpan); List<MockSpan> mockSpans = mockTracer.finishedSpans(); assertEquals(1, mockSpans.size()); assertEquals("query_pointQuery", mockSpans.get(0).operationName()); } assertNull(scopeManager.activeSpan(), "There should be no activeSpan"); }
@Test void testQuery_query() throws ExecutionException, InterruptedException { when(mockQueryBus.query(any(QueryMessage.class))) .thenReturn(CompletableFuture.completedFuture(answer1)); MockSpan span = mockTracer.buildSpan("testQuery_query").start(); ScopeManager scopeManager = mockTracer.scopeManager(); try (final Scope ignored = scopeManager.activate(span)) { CompletableFuture<String> query = testSubject.query( new GenericMessage<>("Query"), String.class); assertEquals("answer1", query.get()); Span activeSpan = mockTracer.activeSpan(); assertEquals(span, activeSpan); List<MockSpan> mockSpans = mockTracer.finishedSpans(); assertEquals(1, mockSpans.size()); assertEquals("query_java.lang.String", mockSpans.get(0).operationName()); assertNotNull(mockSpans.get(0).logEntries()); assertFalse(mockSpans.get(0).logEntries().isEmpty()); assertNotNull(mockSpans.get(0).tags()); assertFalse(mockSpans.get(0).tags().isEmpty()); } assertNull(scopeManager.activeSpan(), "There should be no activeSpan"); }