From c9c857c9084a72de38718d69eec9ca6e115038f7 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Wed, 18 Sep 2024 23:36:30 +0200 Subject: [PATCH] add context info about wrong span or trace (#6703) --- .../sdk/testing/assertj/TraceAssert.java | 3 ++- .../sdk/testing/assertj/TracesAssert.java | 2 +- .../sdk/testing/assertj/TraceAssertionsTest.java | 13 ++++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/TraceAssert.java b/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/TraceAssert.java index 5f3da9db7f4..ee19607e2cd 100644 --- a/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/TraceAssert.java +++ b/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/TraceAssert.java @@ -59,9 +59,10 @@ public TraceAssert hasSpansSatisfyingExactly( List> assertionsList = StreamSupport.stream(assertions.spliterator(), false).collect(Collectors.toList()); hasSize(assertionsList.size()); + // Avoid zipSatisfy - https://github.com/assertj/assertj-core/issues/2300 for (int i = 0; i < assertionsList.size(); i++) { - assertionsList.get(i).accept(new SpanDataAssert(actual.get(i))); + assertionsList.get(i).accept(new SpanDataAssert(actual.get(i)).describedAs("Span " + i)); } return this; } diff --git a/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/TracesAssert.java b/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/TracesAssert.java index e120ae7c854..d4c936fe5b2 100644 --- a/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/TracesAssert.java +++ b/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/TracesAssert.java @@ -105,7 +105,7 @@ public TracesAssert hasTracesSatisfyingExactly( hasSize(assertionsList.size()); // Avoid zipSatisfy - https://github.com/assertj/assertj-core/issues/2300 for (int i = 0; i < assertionsList.size(); i++) { - assertionsList.get(i).accept(new TraceAssert(actual.get(i))); + assertionsList.get(i).accept(new TraceAssert(actual.get(i)).describedAs("Trace " + i)); } return this; } diff --git a/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/TraceAssertionsTest.java b/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/TraceAssertionsTest.java index 825f067fdce..18ac3849690 100644 --- a/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/TraceAssertionsTest.java +++ b/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/TraceAssertionsTest.java @@ -680,6 +680,15 @@ void hasSpansSatisfyingExactly() { trace -> trace.hasSpansSatisfyingExactly( span -> span.hasSpanId(SPAN_ID1), span -> span.hasSpanId(SPAN_ID2))); + // wrong number of spans + assertThatThrownBy( + () -> + TracesAssert.assertThat(traces) + .hasTracesSatisfyingExactly( + trace -> trace.hasSpansSatisfyingExactly(span -> span.hasSpanId(SPAN_ID1)))) + .isInstanceOf(AssertionError.class) + .hasMessageStartingWith("[Trace 0] \n" + "Expected size: 1 but was: 2"); + // test asserting spans in wrong oder assertThatThrownBy( () -> @@ -689,7 +698,9 @@ void hasSpansSatisfyingExactly() { trace.hasSpansSatisfyingExactly( span -> span.hasSpanId(SPAN_ID2), span -> span.hasSpanId(SPAN_ID1)))) - .isInstanceOf(AssertionError.class); + .isInstanceOf(AssertionError.class) + .hasMessage( + "[Span 0] Expected span [span1] to have span ID <0000000000000004> but was <0000000000000003>"); // test asserting spans in any order TracesAssert.assertThat(traces)