Skip to content

Commit

Permalink
Add ObservationContextAssert error assertions (#3267)
Browse files Browse the repository at this point in the history
This change adds assertions allowing to validate the usage of
Observation#error(Throwable):
 - `doesNotHaveError()`
 - `hasError()` (for any error)
 - `hasError(Throwable)` (for a specific error)
  • Loading branch information
simonbasle authored Jul 6, 2022
1 parent 5f2bd00 commit 21d450b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
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

0 comments on commit 21d450b

Please sign in to comment.