diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java index 5653673bca..c92668319f 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java @@ -1332,6 +1332,23 @@ StepVerifier.Step after(StepVerifier.Step step, T object) { } } + /** Avoid list collection when verifying that a {@link Flux} emits exactly one value. */ + // XXX: This rule assumes that the matched collector does not drop elements. Consider introducing + // a `@Matches(DoesNotDropElements.class)` or `@NotMatches(MayDropElements.class)` guard. + static final class FluxAsStepVerifierExpectNext> { + @BeforeTemplate + StepVerifier.Step before(Flux flux, Collector listCollector, T object) { + return flux.collect(listCollector) + .as(StepVerifier::create) + .assertNext(list -> assertThat(list).containsExactly(object)); + } + + @AfterTemplate + StepVerifier.Step after(Flux flux, T object) { + return flux.as(StepVerifier::create).expectNext(object); + } + } + /** Prefer {@link StepVerifier.LastStep#verifyComplete()} over more verbose alternatives. */ static final class StepVerifierLastStepVerifyComplete { @BeforeTemplate diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java index 1317776baa..7f8021f70c 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java @@ -422,6 +422,13 @@ ImmutableSet> testStepVerifierStepExpectNext() { StepVerifier.create(Mono.just("baz")).expectNextMatches("qux"::equals)); } + StepVerifier.Step testFluxAsStepVerifierExpectNext() { + return Flux.just(1) + .collect(toImmutableList()) + .as(StepVerifier::create) + .assertNext(list -> assertThat(list).containsExactly(2)); + } + Duration testStepVerifierLastStepVerifyComplete() { return StepVerifier.create(Mono.empty()).expectComplete().verify(); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java index d52b8dd180..b12af508c4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java @@ -413,6 +413,10 @@ ImmutableSet> testStepVerifierStepExpectNext() { StepVerifier.create(Mono.just("baz")).expectNext("qux")); } + StepVerifier.Step testFluxAsStepVerifierExpectNext() { + return Flux.just(1).as(StepVerifier::create).expectNext(2); + } + Duration testStepVerifierLastStepVerifyComplete() { return StepVerifier.create(Mono.empty()).verifyComplete(); }