Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can´t mock brave.Tracer #50

Open
dotarsoyak opened this issue Jun 23, 2024 · 0 comments
Open

can´t mock brave.Tracer #50

dotarsoyak opened this issue Jun 23, 2024 · 0 comments

Comments

@dotarsoyak
Copy link

dotarsoyak commented Jun 23, 2024

Hi

I´m trying to mock brave.Tracer. to get Tracer.currenSpan(), but I can´t get it, here is my code.

I´m usign:

  • io.zipkin.brave:5.13.9
  • spring-cloud-sleuth-zipkin:3.1.3

could you give me some advice?

my test method

import brave.Span;
import brave.Tracer;
import brave.Tracing;

@ExtendedWith(SpringExtension.class)
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM.PORT,
classes = {Main.class, TestConfig.class})
public class TestingClass{
  
  @Autowired
  private Tracer tracer;

  @Test
  public void must_return_currentSpan() throws Exception {
    brave.Span span = Tracing.newBuilder().build().tracer().nextSpan().name("span").start();

    doReturn(span).when(tracer).currentSpan();

    mvc.perform(get("the url"));  //this is only contextual, an example to avoid write all code.
  }
}

the class to test

import brave.Span;

@Service
@RequiredArgsConstructor
public class TheClass {
  private final Tracer tracer;

  public Span generateSpan() {
    return tracer.currentSpan();
  }

}

In some point after execute the test method, I get this error

java.lang.NullPointerException: Cannot invoke "brave.Tracer$SpanInScope.close()" because "this.delegate" is null 
at org.springframework.cloud.sleuth.brave.bridge.BraveSpanInScope.close(BraveTracer.java:153)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant