I’ve spent several days trying to mock “brave.Tracer” as below to get “brave.Span” when the code calls “tracer.currentSpan();”
I´m usign
io.zipkin.brave:brave:5.13.9
org.springframework.cloud 3.1.13
**I can´t share the original code because company politics, but this is a fiel representation.
but I only get this error, do you have any idea, to solve this issue?, thanks in advance.
My code:
@ExtendWith(SpringExtension.class)
public class TestableClass{
@Autowired private brave.Tracer tracerBean;
@MockBean private brave.Tracer tracerMock;
@Test
public void methodTest{
tracerBean = Tracing.newBuilder().build().tracer();
doReturn(tracerBean.currentSpan()).when(tracerMock).currentSpan();
}
}
The error message
Exception in thread "pool-2-thread-1" java.lang.NullPointerException: Cannot invoke "brave.Span.name(String)" because "this.delegate" is nullat org.springframework.cloud.sleuth.brave.bridge.BraveSpan.name(BraveSpan.java:60)at org.springframework.cloud.sleuth.docs.AssertingSpan.name(AssertingSpan.java:92)at org.springframework.cloud.sleuth.instrument.async.TraceRunnable.run(TraceRunnable.java:62)at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)at java.base/java.lang.Thread.run(Thread.java:840)2024-06-22 11:35:06.645 [INFO ] 20208 --- [ scheduling-1] brave.Tracer : {"traceId":"000000000000000a","parentId":"000000000000000a","id":"5b679d3848eacf62","name":"execute","timestamp":1719077706634900,"duration":8331,"localEndpoint":{"serviceName":"unknown","ipv4":"10.39.30.49"},"tags":{"class":"IdleConnectionMonitor","method":"execute","error":"Cannot invoke "brave.Tracer$SpanInScope.close()" because "this.delegate" is null"}} []2024-06-22 11:35:06.647 [ERROR ] 20208 --- [ scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task []
java.lang.NullPointerException: Cannot invoke "brave.Tracer$SpanInScope.close()" because "this.delegate" is nullat org.springframework.cloud.sleuth.brave.bridge.BraveSpanInScope.close(BraveTracer.java:153)at org.springframework.cloud.sleuth.instrument.scheduling.TraceSchedulingAspect.traceBackgroundThread(TraceSchedulingAspect.java:71)at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.base/java.lang.reflect.Method.invoke(Method.java:568)at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:634)
I´ve tried several ways to solve this issue, for example:
-tried to mock the brave.Tracer with @MockBean but doesn´t generates the brave.Span it only returns null.
-It doesn´t work with “post” requests for example, perform(post(…)), but definitly it works with get requests, and I don´t know why.
-When I launch my request from postman everything goes ok, when I debug from postman the brave.Span is not null when the app calls tracer.currentSpan(), and that´s fine.
-But when I send the request from test integration method, the brave.Span is null when the app calls tracer.currentSpan().
When I use @Autowired brave.Tracer thats is the only way when the brave.Span is not null when the app calls tracer.currentSpan(), but I get the above error.