Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,32 @@ public SELF doesNotHaveMapEntry(Object key, Object value) {
return (SELF) this;
}

public SELF doesNotHaveError() {
isNotNull();
Optional<Throwable> error = this.actual.getError();
error.ifPresent(throwable -> failWithMessage("Observation should not have an error, found <%s>", throwable));
return (SELF) this;
}

public SELF hasError() {
isNotNull();
Optional<Throwable> error = this.actual.getError();
if (!error.isPresent()) {
failWithMessage("Observation should have an error, but none was found");
}
return (SELF) this;
}

public SELF hasError(Throwable expectedError) {
isNotNull();
hasError();
Throwable error = this.actual.getError().get();
if (!error.equals(expectedError)) {
failWithMessage("Observation expected to have error <%s>, but has <%s>", expectedError, error);
}
return (SELF) this;
}

public ObservationContextAssertReturningThrowableAssert assertThatThrowable() {
return new ObservationContextAssertReturningThrowableAssert(actual.getError().orElse(null), this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,73 @@ void should_not_throw_exception_when_map_entry_missing() {
thenNoException().isThrownBy(() -> assertThat(context).doesNotHaveMapEntry("foo", "bar"));
}

@Test
void should_not_throw_when_does_not_have_error() {
thenNoException().isThrownBy(() -> assertThat(context).doesNotHaveError());
}

@Test
void should_throw_when_unexpected_error() {
Throwable expected = new IllegalStateException("test");

registry.observationConfig().observationHandler(c -> true);
Observation observation = Observation.start("foo", context, registry);
observation.error(expected);

thenThrownBy(() -> assertThat(context).doesNotHaveError())
.hasMessage("Observation should not have an error, found <java.lang.IllegalStateException: test>");
}

@Test
void should_not_throw_when_has_error() {
Throwable expected = new IllegalStateException("test");

registry.observationConfig().observationHandler(c -> true);
Observation observation = Observation.start("foo", context, registry);
observation.error(expected);

thenNoException().isThrownBy(() -> assertThat(context).hasError());
}

@Test
void should_throw_when_has_error_missing() {
thenThrownBy(() -> assertThat(context).hasError())
.hasMessage("Observation should have an error, but none was found");
}

@Test
void should_not_throw_when_has_specific_error() {
Throwable expected = new IllegalStateException("test");

registry.observationConfig().observationHandler(c -> true);
Observation observation = Observation.start("foo", context, registry);
observation.error(expected);

thenNoException().isThrownBy(() -> assertThat(context).hasError(expected));
}

@Test
void should_throw_when_has_specific_error_missing() {
Throwable expected = new IllegalStateException("test");

thenThrownBy(() -> assertThat(context).hasError(expected))
.hasMessage("Observation should have an error, but none was found");
}

@Test
void should_throw_when_has_specific_error_does_not_match() {
Throwable expected = new IllegalStateException("test expected");
Throwable actual = new IllegalArgumentException("test actual");

registry.observationConfig().observationHandler(c -> true);
Observation observation = Observation.start("foo", context, registry);
observation.error(actual);

thenThrownBy(() -> assertThat(context).hasError(expected))
.hasMessage("Observation expected to have error <java.lang.IllegalStateException: test expected>,"
+ " but has <java.lang.IllegalArgumentException: test actual>");
}

@Test
void should_jump_to_and_back_from_throwable_assert() {
context.setName("foo").setError(new RuntimeException("bar"));
Expand Down