Skip to content

Commit

Permalink
Add ability to check if a HealthCheck.Result is healthy using a boole…
Browse files Browse the repository at this point in the history
…an (#498)

* Add hasHealthyValue(boolean) to HealthCheckResultAssertions

Closes #494
  • Loading branch information
sleberknight authored Jun 19, 2024
1 parent ef36863 commit 7064ab2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ public HealthCheckResultAssertions isUnhealthy() {
return this;
}

/**
* Asserts the health check result returns the given boolean value from {@link HealthCheck.Result#isHealthy}.
*
* @param value the expected value
* @return this instance
*/
public HealthCheckResultAssertions hasHealthyValue(boolean value) {
Assertions.assertThat(result.isHealthy())
.describedAs("Expected result to return %b from isHealthy", value)
.isEqualTo(value);

return this;
}

/**
* Asserts the health check result has the given message.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,29 @@ void shouldFail_WhenHealthy() {
}
}

@Nested
class HasHealthyValue {

@ParameterizedTest
@ValueSource(booleans = { true, false })
void shouldPass_WhenReturnsExpectedValue(boolean healthy) {
var healthCheck = MockHealthCheck.builder().healthy(healthy).build();

assertThatCode(() -> assertThat(healthCheck).hasHealthyValue(healthy))
.doesNotThrowAnyException();
}

@ParameterizedTest
@ValueSource(booleans = { true, false })
void shouldFail_WhenDoesNotReturnExpectedValue(boolean healthy) {
var healthCheck = MockHealthCheck.builder().healthy(!healthy).build();

assertThatThrownBy(() -> assertThat(healthCheck).hasHealthyValue(healthy))
.isNotNull()
.hasMessageContaining("Expected result to return %b from isHealthy", healthy);
}
}

@Nested
class HasMessageMethods {

Expand Down

0 comments on commit 7064ab2

Please sign in to comment.