Skip to content

Commit

Permalink
fix #43 Convenience expect-and-verify methods
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbasle committed Dec 5, 2016
1 parent 310ed27 commit f607e3d
Show file tree
Hide file tree
Showing 3 changed files with 195 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
80 changes: 80 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,84 @@ public void initialBoundedThenUnboundedRequestDoesntOverflow() {
.verify();
}

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

StepVerifier.create(Flux.error(new IllegalArgumentException()))
.verifyError();
}

@Test
public void verifyErrorClassTriggersVerification() {
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());
}

StepVerifier.create(Flux.error(new IllegalArgumentException()))
.verifyError(IllegalArgumentException.class);
}

@Test
public void verifyErrorMessageTriggersVerification() {
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());
}

StepVerifier.create(Flux.error(new IllegalArgumentException("boom")))
.verifyErrorMessage("boom");
}

@Test
public void verifyErrorPredicateTriggersVerification() {
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());
}

StepVerifier.create(Flux.error(new IllegalArgumentException("boom")))
.verifyErrorMatches(e -> e instanceof IllegalArgumentException);
}

@Test
public void verifyCompleteTriggersVerification() {
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());
}

StepVerifier.create(Flux.just(1, 2))
.expectNext(1, 2)
.verifyComplete();
}

}

0 comments on commit f607e3d

Please sign in to comment.