2

We have a spring boot rest api (spring boot 2.3.0.RELEASE) that uses spring cloud sleuth (version 2.2.3.RELEASE).

At some point, we use the trace id from spring sleuth as data. The trace id is fetched by autowiring the Tracing bean and then accessing the current span. Lets say we defined a bean SimpleCorrelationBean with:

@Autowired
private Tracer tracer;

public String getCorrelationId() {
    return tracer.currentSpan().context().traceIdString();
} 

This seem to work perfectly when running the spring boot application, but when we try to access the tracer.currentSpan() in the unit tests, this is null. It looks like spring cloud sleuth is not creating any span while running tests..

I think it has something to do with the application context that is set up during the unit test, but I don't know how to enable spring cloud sleuth for the test application context.

Below is a simple test class where the error occurs in simpleTest1. In simpleTest2, no error occurs.

simpleTest1 errors because tracer.currentSpan() is null

@ExtendWith({ RestDocumentationExtension.class, SpringExtension.class })
@SpringBootTest(classes = MusicService.class)
@WebAppConfiguration
@ActiveProfiles("unit-test")
@ComponentScan(basePackageClasses = datacast2.data.JpaConfig.class)
public class SimpleTest {
    
    private static final Logger logger = LoggerFactory.getLogger(SimpleTest.class);
    
    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;
    
    @Autowired
    private FilterChainProxy springSecurityFilterChain;
    
    @Autowired
    private SimpleCorrelationBean simpleCorrelationBean;
    
    @Autowired
    private Tracer tracer;
    
    @BeforeEach
    public void setup(RestDocumentationContextProvider restDocumentation) throws Exception {

        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
                .apply(documentationConfiguration(restDocumentation))
                .addFilter(springSecurityFilterChain).build();

    }
    
    @Test
    public void simpleTest1() throws Exception {
        try {
            String correlationId = simpleCorrelationBean.getCorrelationId();
        }catch(Exception e) {
            logger.error("This seem to fail.", e);
        }
        
    }
    
    @Test
    public void simpleTest2() throws Exception {
        
        //It looks like spring cloud sleuth is not creating a span, so we create one ourselfs
        Span newSpan = this.tracer.nextSpan().name("simpleTest2");
                
        try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(newSpan.start())) {
            String correlationId = simpleCorrelationBean.getCorrelationId();
        }
        finally {
            newSpan.finish();
        }
    }

}

The question is: how to enable spring cloud sleuth for a mockMvc during unit tests?

1 Answer 1

0

The issue here is that MockMvc is created manually instead of relying on autoconfiguration. In this particular case custom configuration of MockMvc could be necessary. However, at least for my version of Spring Boot (2.7.6), there is no need to manually configure MockMvc, even though I use Spring Security and Spring Security Test. I couldn't figure out how to enable tracing when manually configuring MockMvc though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.