Skip to content

Commit

Permalink
fix 43 Convenience expect-and-verify methods
Browse files Browse the repository at this point in the history
This commit adds methods that combine `expectError`/`expectComplete`
with a call to the simple `.verify()` variant of `StepVerifier`.

For more advanced usage where one desires to have logging or timeout,
the usual expect method followed by the relevant `log()`,`verify()` or
`verify(Duration)` call should be used instead.

(fix reactor/reactor-addons#43)
  • Loading branch information
simonbasle committed Dec 7, 2016
1 parent 61bada7 commit 5d4ac1d
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,31 @@ public DefaultStepVerifier<T> thenCancel() {
return build();
}

@Override
public Duration verifyError() {
return expectError().verify();
}

@Override
public Duration verifyError(Class<? extends Throwable> clazz) {
return expectError(clazz).verify();
}

@Override
public Duration verifyErrorMessage(String errorMessage) {
return expectErrorMessage(errorMessage).verify();
}

@Override
public Duration verifyErrorMatches(Predicate<Throwable> predicate) {
return expectErrorMatches(predicate).verify();
}

@Override
public Duration verifyComplete() {
return expectComplete().verify();
}

@Override
public DefaultStepVerifierBuilder<T> thenRequest(long n) {
checkStrictlyPositive(n);
Expand Down
90 changes: 90 additions & 0 deletions reactor-test/src/main/java/reactor/test/StepVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,96 @@ interface LastStep {
* @see Subscription#cancel()
*/
StepVerifier thenCancel();

/**
* Trigger the {@link #verify() verification}, expecting an unspecified error
* as terminal event.
* <p>
* This is a convenience method that calls {@link #verify()} in addition to the
* expectation. Explicitly use the expect method and verification method
* separately if you need something more specific (like activating logging or
* putting a timeout).
*
* @return the {@link Duration} of the verification
*
* @see #expectError()
* @see #verify()
* @see Subscriber#onError(Throwable)
*/
Duration verifyError();

/**
* Trigger the {@link #verify() verification}, expecting an error of the specified
* type as terminal event.
* <p>
* This is a convenience method that calls {@link #verify()} in addition to the
* expectation. Explicitly use the expect method and verification method
* separately if you need something more specific (like activating logging or
* putting a timeout).
*
* @param clazz the expected error type
* @return the {@link Duration} of the verification
*
* @see #expectError(Class)
* @see #verify()
* @see Subscriber#onError(Throwable)
*/
Duration verifyError(Class<? extends Throwable> clazz);

/**
* Trigger the {@link #verify() verification}, expecting an error with the
* specified messagee as terminal event.
* <p>
* This is a convenience method that calls {@link #verify()} in addition to the
* expectation. Explicitly use the expect method and verification method
* separately if you need something more specific (like activating logging or
* putting a timeout).
*
* @param errorMessage the expected error message
* @return the {@link Duration} of the verification
*
* @see #expectErrorMessage(String)
* @see #verify()
* @see Subscriber#onError(Throwable)
*/
Duration verifyErrorMessage(String errorMessage);

/**
* Trigger the {@link #verify() verification}, expecting an error that matches
* the given predicate as terminal event.
* <p>
* This is a convenience method that calls {@link #verify()} in addition to the
* expectation. Explicitly use the expect method and verification method
* separately if you need something more specific (like activating logging or
* putting a timeout).
*
* @param predicate the predicate to test on the next received error
* @return the {@link Duration} of the verification
*
* @see #expectErrorMatches(Predicate)
* @see #verify()
* @see Subscriber#onError(Throwable)
*/
Duration verifyErrorMatches(Predicate<Throwable> predicate);

/**
* Trigger the {@link #verify() verification}, expecting a completion signal
* as terminal event.
* <p>
* This is a convenience method that calls {@link #verify()} in addition to the
* expectation. Explicitly use the expect method and verification method
* separately if you need something more specific (like activating logging or
* putting a timeout).
*
* @return the {@link Duration} of the verification
*
* @see #expectComplete()
* @see #verify()
* @see Subscriber#onComplete()
*
*/
Duration verifyComplete();

}

/**
Expand Down
97 changes: 97 additions & 0 deletions reactor-test/src/test/java/reactor/test/StepVerifierTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1227,4 +1227,101 @@ public void initialBoundedThenUnboundedRequestDoesntOverflow() {
.verify();
}

@Test
public void verifyErrorTriggersVerificationFail() {
try {
StepVerifier.create(Flux.empty())
.verifyError();
fail("expected verifyError to fail");
}
catch (AssertionError e) {
assertEquals(
"expectation \"expectError()\" failed (expected: onError(); actual: onComplete())",
e.getMessage());
}
}

@Test
public void verifyErrorTriggersVerificationSuccess() {
StepVerifier.create(Flux.error(new IllegalArgumentException()))
.verifyError();
}

@Test
public void verifyErrorClassTriggersVerificationFail() {
try {
StepVerifier.create(Flux.empty())
.verifyError(IllegalArgumentException.class);
fail("expected verifyError to fail");
}
catch (AssertionError e) {
assertEquals(
"expectation \"expectError(Class)\" failed (expected: onError(IllegalArgumentException); actual: onComplete())",
e.getMessage());
}
}

@Test
public void verifyErrorClassTriggersVerificationSuccess() {
StepVerifier.create(Flux.error(new IllegalArgumentException()))
.verifyError(IllegalArgumentException.class);
}

@Test
public void verifyErrorMessageTriggersVerificationFail() {
try {
StepVerifier.create(Flux.empty())
.verifyErrorMessage("boom");
fail("expected verifyError to fail");
}
catch (AssertionError e) {
assertEquals("expectation \"expectErrorMessage\" failed (expected: onError(\"boom\"); actual: onComplete())",
e.getMessage());
}
}

@Test
public void verifyErrorMessageTriggersVerificationSuccess() {
StepVerifier.create(Flux.error(new IllegalArgumentException("boom")))
.verifyErrorMessage("boom");
}

@Test
public void verifyErrorPredicateTriggersVerificationFail() {
try {
StepVerifier.create(Flux.empty())
.verifyErrorMatches(e -> e instanceof IllegalArgumentException);
fail("expected verifyError to fail");
}
catch (AssertionError e) {
assertEquals("expectation \"expectErrorMatches\" failed (expected: onError(); actual: onComplete())",
e.getMessage());
}
}

@Test
public void verifyErrorPredicateTriggersVerificationSuccess() {
StepVerifier.create(Flux.error(new IllegalArgumentException("boom")))
.verifyErrorMatches(e -> e instanceof IllegalArgumentException);
}

@Test
public void verifyCompleteTriggersVerificationFail() {
try {
StepVerifier.create(Flux.error(new IllegalArgumentException()))
.verifyComplete();
fail("expected verifyComplete to fail");
}
catch (AssertionError e) {
assertEquals("expectation \"expectComplete\" failed (expected: onComplete(); actual: onError(java.lang.IllegalArgumentException))", e.getMessage());
}
}

@Test
public void verifyCompleteTriggersVerificationSuccess() {
StepVerifier.create(Flux.just(1, 2))
.expectNext(1, 2)
.verifyComplete();
}

}

0 comments on commit 5d4ac1d

Please sign in to comment.