Skip to content

Commit 6054204

Browse files
authored
Add assertion support for Span hasException(null) (#8033)
1 parent 619ccc5 commit 6054204

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/SpanDataAssert.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,19 +375,28 @@ public SpanDataAssert hasAttributesSatisfyingExactly(Iterable<AttributeAssertion
375375

376376
/**
377377
* Asserts the span has an exception event for the given {@link Throwable}. The stack trace is not
378-
* matched against.
378+
* matched against. If {@code exception} is {@code null}, asserts the span has no exception event.
379379
*/
380380
// Workaround "passing @Nullable parameter 'stackTrace' where @NonNull is required", Nullaway
381381
// seems to think assertThat is supposed to be passed NonNull even though we know that can't be
382382
// true for assertions.
383383
@SuppressWarnings("NullAway")
384-
public SpanDataAssert hasException(Throwable exception) {
384+
public SpanDataAssert hasException(@Nullable Throwable exception) {
385385
EventData exceptionEvent =
386386
actual.getEvents().stream()
387387
.filter(event -> event.getName().equals(EXCEPTION_EVENT_NAME))
388388
.findFirst()
389389
.orElse(null);
390390

391+
if (exception == null) {
392+
if (exceptionEvent != null) {
393+
failWithMessage(
394+
"Expected span [%s] to have no exception event but had <%s>",
395+
actual.getName(), exceptionEvent);
396+
}
397+
return this;
398+
}
399+
391400
if (exceptionEvent == null) {
392401
failWithMessage(
393402
"Expected span [%s] to have an exception event but only had events <%s>",

sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/TraceAssertionsTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ void passing() {
301301

302302
assertThat(RESOURCE.getAttributes())
303303
.containsOnly(entry(DOG, "bark"), entry(AttributeKey.booleanKey("dog is cute"), true));
304+
305+
assertThat(buildTestSpan(SPAN_ID1, "span")).hasException(null);
304306
}
305307

306308
@Test
@@ -564,6 +566,8 @@ void failure() {
564566
assertThatThrownBy(
565567
() -> assertThat(SPAN1).hasException(new IllegalArgumentException("good argument")))
566568
.isInstanceOf(AssertionError.class);
569+
assertThatThrownBy(() -> assertThat(SPAN1).hasException(null))
570+
.isInstanceOf(AssertionError.class);
567571
assertThatThrownBy(() -> assertThat(SPAN1).hasLinks()).isInstanceOf(AssertionError.class);
568572
assertThatThrownBy(() -> assertThat(SPAN1).hasLinks(Collections.emptyList()))
569573
.isInstanceOf(AssertionError.class);

0 commit comments

Comments
 (0)