0

I'm trying to write a simple test for an application using Spring Boot 3 + Micrometer Tracing

Rest Controller:

@RestController
@RequestMapping("/customers2")
@Slf4j
public class CustomerController {

    @Autowired
    WebClient.Builder webClientBuilder;

    @GetMapping
    public String hello() {
        log.info("Hello Controller2 called...");

        webClientBuilder
                .baseUrl("http://localhost:8080")
                .build()
                .method(HttpMethod.GET)
                .uri(uriBuilder -> uriBuilder.path("/customers").build())
                .exchange()
                .block();

        return "Hello World2";
    }

Simplest test class ever

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@EnableAutoConfiguration
@EnableTestObservation
class DemoApplicationTests {

    @Autowired
    SimpleTracer tracer;

    @Autowired
    WebClient.Builder webClientBuilder;

    @Test
    void contextLoads() {
        webClientBuilder
                .build()
                .method(HttpMethod.GET)
                .uri(uriBuilder -> uriBuilder.path("/customers2").build())
                .exchange()
                .block();

        System.out.println("hola");
    }

}

Configuration for the test:

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@AutoConfigureObservability
@Import({
        EnableTestObservation.ObservationTestConfiguration.class
})
public @interface EnableTestObservation {

    @TestConfiguration
    class ObservationTestConfiguration {

        @Bean
        SimpleTracer simpleTracer() {
            return new SimpleTracer();
        }

    }

}

When I run the test I'm getting this:

class io.micrometer.tracing.test.simple.SimpleTraceContext cannot be cast to class io.micrometer.tracing.brave.bridge.BraveTraceContext (io.micrometer.tracing.test.simple.SimpleTraceContext and io.micrometer.tracing.brave.bridge.BraveTraceContext are in unnamed module of loader 'app')

kerbermeister
  • 2,985
  • 3
  • 11
  • 30
Carlos Gonzalez
  • 607
  • 1
  • 5
  • 13

1 Answers1

0

I could not get SimpleTracer to work, but I was able to get the tests working by changing the EnableTestObservation annotation. I implemented the Tracer bean as a Brave Tracer vs. SimpleTracer.

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@AutoConfigureObservability
public @interface EnableTestObservation {

  @TestConfiguration
  class ObservationTestConfiguration {

    @Autowired
    ObservationRegistry observationRegistry;

    @Bean
    Tracer simpleTracer() {
      return ZipkinBraveSetup.builder()
          .register(observationRegistry)
          .getBuildingBlocks()
          .getTracer();
    }
  }

}

I had to also remove some dependencies in the pom, so that the correct beans would be found to wire the Tracer. For some reason, multiple tracer implementations were on the test classpath. I just wanted to use Brave, so I excluded the others:

<dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-tracing-integration-test</artifactId>
      <exclusions>
        <exclusion>
          <groupId>io.micrometer</groupId>
          <artifactId>micrometer-tracing-bridge-otel</artifactId>
        </exclusion>
        <exclusion>
          <groupId>io.micrometer</groupId>
          <artifactId>micrometer-tracing-reporter-wavefront</artifactId>
        </exclusion>
        <exclusion>
          <groupId>io.zipkin.reporter2</groupId>
          <artifactId>zipkin-reporter</artifactId>
        </exclusion>
      </exclusions>
      <scope>test</scope>
    </dependency>