TraceUnit is a library that helps with Opentelemetry tracing mocking/testing
Add dependency to your project
For maven:
<dependency>
<groupId>io.github.traceunit</groupId>
<artifactId>traceunit-junit5</artifactId>
<version>0.0.4</version>
<scope>test</scope>
</dependency>
For Gradle:
testImplementation 'io.github.traceunit:traceunit-junit5:0.0.4'
- Extend your test class with
TraceUnitExtension
:
@ExtendWith({TraceUnitExtension.class})
Span.current()
will be automatically mocked after this step
- Annotate Tracer mock with
@MockTracer
@MockTracer
Tracer tracer;
- Inject mocked item into any test class:
@BeforeEach
void setUp() {
testService = new TestService();
testService.tracer = tracer;