Skip to content

Commit

Permalink
Introduce StepVerifierLastStepVerifyErrorSatisfiesAssertJ Refaster …
Browse files Browse the repository at this point in the history
…rule
  • Loading branch information
werli committed Apr 11, 2024
1 parent e0c357a commit f4ab9b4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collector;
import org.assertj.core.api.Assertions;
import org.jspecify.annotations.Nullable;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -1928,6 +1929,30 @@ Duration after(StepVerifier.LastStep step, Consumer<Throwable> consumer) {
}
}

/**
* Prefer {@link StepVerifier.LastStep#verifyErrorSatisfies(Consumer)} with AssertJ over more
* contrived alternatives.
*/
static final class StepVerifierLastStepVerifyErrorSatisfiesAssertJ {
@BeforeTemplate
void before(StepVerifier.LastStep step, Class<? extends Throwable> clazz, String message) {
Refaster.anyOf(
step.expectError()
.verifyThenAssertThat()
.hasOperatorErrorOfType(clazz)
.hasOperatorErrorWithMessage(message),
step.expectError(clazz).verifyThenAssertThat().hasOperatorErrorWithMessage(message),
step.expectErrorMessage(message).verifyThenAssertThat().hasOperatorErrorOfType(clazz));
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(StepVerifier.LastStep step, Class<? extends Throwable> clazz, String message) {
step.verifyErrorSatisfies(
t -> Assertions.assertThat(t).isInstanceOf(clazz).hasMessage(message));
}
}

/**
* Prefer {@link StepVerifier.LastStep#verifyErrorMessage(String)} over more verbose alternatives.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,25 @@ Duration testStepVerifierLastStepVerifyErrorSatisfies() {
return Mono.empty().as(StepVerifier::create).expectErrorSatisfies(t -> {}).verify();
}

void testStepVerifierLastStepVerifyErrorSatisfiesAssertJ() {
Mono.empty()
.as(StepVerifier::create)
.expectError()
.verifyThenAssertThat()
.hasOperatorErrorOfType(IllegalStateException.class)
.hasOperatorErrorWithMessage("foo");
Mono.empty()
.as(StepVerifier::create)
.expectError(IllegalStateException.class)
.verifyThenAssertThat()
.hasOperatorErrorWithMessage("bar");
Mono.empty()
.as(StepVerifier::create)
.expectErrorMessage("baz")
.verifyThenAssertThat()
.hasOperatorErrorOfType(IllegalStateException.class);
}

Duration testStepVerifierLastStepVerifyErrorMessage() {
return Mono.empty().as(StepVerifier::create).expectErrorMessage("foo").verify();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,21 @@ Duration testStepVerifierLastStepVerifyErrorSatisfies() {
return Mono.empty().as(StepVerifier::create).verifyErrorSatisfies(t -> {});
}

void testStepVerifierLastStepVerifyErrorSatisfiesAssertJ() {
Mono.empty()
.as(StepVerifier::create)
.verifyErrorSatisfies(
t -> assertThat(t).isInstanceOf(IllegalStateException.class).hasMessage("foo"));
Mono.empty()
.as(StepVerifier::create)
.verifyErrorSatisfies(
t -> assertThat(t).isInstanceOf(IllegalStateException.class).hasMessage("bar"));
Mono.empty()
.as(StepVerifier::create)
.verifyErrorSatisfies(
t -> assertThat(t).isInstanceOf(IllegalStateException.class).hasMessage("baz"));
}

Duration testStepVerifierLastStepVerifyErrorMessage() {
return Mono.empty().as(StepVerifier::create).verifyErrorMessage("foo");
}
Expand Down

0 comments on commit f4ab9b4

Please sign in to comment.