Class OpenTelemetryRule
- java.lang.Object
-
- org.junit.rules.ExternalResource
-
- io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule
-
- All Implemented Interfaces:
org.junit.rules.TestRule
public final class OpenTelemetryRule extends org.junit.rules.ExternalResourceA JUnit4 rule which sets up theOpenTelemetrySdkfor testing, resetting state between tests. This rule cannot be used withClassRule.// public class CoolTest { // @Rule // public OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); // // private Tracer tracer; // // @Before // public void setUp() { // tracer = otelTesting.getOpenTelemetry().getTracer("test"); // } // // @Test // public void test() { // tracer.spanBuilder("name").startSpan().end(); // assertThat(otelTesting.getSpans()).containsExactly(expected); // } // }
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafter()protected voidbefore()voidclearSpans()Clears the collected exportedSpanData.static OpenTelemetryRulecreate()Returns aOpenTelemetryRulewith a default SDK initialized with an in-memory span exporter and W3C trace context propagation.io.opentelemetry.api.OpenTelemetrygetOpenTelemetry()Returns theOpenTelemetrySdkcreated by this extension.List<io.opentelemetry.sdk.trace.data.SpanData>getSpans()Returns all the exportedSpanDataso far.
-
-
-
Method Detail
-
create
public static OpenTelemetryRule create()
Returns aOpenTelemetryRulewith a default SDK initialized with an in-memory span exporter and W3C trace context propagation.
-
getOpenTelemetry
public io.opentelemetry.api.OpenTelemetry getOpenTelemetry()
Returns theOpenTelemetrySdkcreated by this extension.
-
getSpans
public List<io.opentelemetry.sdk.trace.data.SpanData> getSpans()
Returns all the exportedSpanDataso far.
-
clearSpans
public void clearSpans()
Clears the collected exportedSpanData. Consider making your test smaller instead of manually clearing state using this method.
-
before
protected void before()
- Overrides:
beforein classorg.junit.rules.ExternalResource
-
after
protected void after()
- Overrides:
afterin classorg.junit.rules.ExternalResource
-
-