Package io.vertx.junit5
Class VertxTestContext
- java.lang.Object
-
- io.vertx.junit5.VertxTestContext
-
public final class VertxTestContext extends Object
A test context to wait on the outcomes of asynchronous operations.- Author:
- Julien Ponge
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceVertxTestContext.ExecutionBlockInterface for an executable block of assertion code.
-
Constructor Summary
Constructors Constructor Description VertxTestContext()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <T> Future<T>assertComplete(Future<T> fut)This method allows you to check if a future is completed.<T> Future<T>assertFailure(Future<T> fut)This method allows you to check if a future is failed.booleanawaitCompletion(long timeout, TimeUnit unit)Wait for the completion of the test context.ThrowablecauseOfFailure()Give the cause of failure.Checkpointcheckpoint()Create a strict checkpoint.Checkpointcheckpoint(int requiredNumberOfPasses)Create a strict checkpoint.booleancompleted()Check if the context has completed.voidcompleteNow()Complete the test context immediately, making the corresponding test pass.<T> Handler<AsyncResult<T>>completing()Deprecated.UsesucceedingThenComplete()instead.booleanfailed()Check if the context has been marked has failed or not.<T> Handler<AsyncResult<T>>failing()Deprecated.UsefailingThenComplete()orfailing(Handler), for examplefailing(e -> checkpoint.flag()),failing(e -> { more testing code }), orfailing(e -> {}).<T> Handler<AsyncResult<T>>failing(Handler<Throwable> nextHandler)Create an asynchronous result handler that expects a failure, and passes the exception to another handler.<T> Handler<AsyncResult<T>>failingThenComplete()Create an asynchronous result handler that expects a failure to then complete the test context.voidfailNow(String message)CallsfailNow(Throwable)with themessage.voidfailNow(Throwable t)Make the test context fail immediately, making the corresponding test fail.CheckpointlaxCheckpoint()Create a lax checkpoint.CheckpointlaxCheckpoint(int requiredNumberOfPasses)Create a lax checkpoint.<T> Handler<AsyncResult<T>>succeeding(Handler<T> nextHandler)Create an asynchronous result handler that expects a success, and passes the value to another handler.<T> Handler<AsyncResult<T>>succeedingThenComplete()Create an asynchronous result handler that expects a success to then complete the test context.Set<StackTraceElement>unsatisfiedCheckpointCallSites()Gives the call sites of all unsatisfied checkpoints.VertxTestContextverify(VertxTestContext.ExecutionBlock block)Allow verifications and assertions to be made.
-
-
-
Method Detail
-
failed
public boolean failed()
Check if the context has been marked has failed or not.- Returns:
trueif the context has failed,falseotherwise.
-
causeOfFailure
public Throwable causeOfFailure()
Give the cause of failure.- Returns:
- the cause of failure, or
nullif the test context hasn't failed.
-
completed
public boolean completed()
Check if the context has completed.- Returns:
trueif the context has completed,falseotherwise.
-
unsatisfiedCheckpointCallSites
public Set<StackTraceElement> unsatisfiedCheckpointCallSites()
Gives the call sites of all unsatisfied checkpoints.- Returns:
- a set of
StackTraceElementreferences pointing to the unsatisfied checkpoint call sites.
-
completeNow
public void completeNow()
Complete the test context immediately, making the corresponding test pass.
-
failNow
public void failNow(Throwable t)
Make the test context fail immediately, making the corresponding test fail.- Parameters:
t- the cause of failure.
-
failNow
public void failNow(String message)
CallsfailNow(Throwable)with themessage.- Parameters:
message- the cause of failure
-
laxCheckpoint
public Checkpoint laxCheckpoint()
Create a lax checkpoint.- Returns:
- a checkpoint that requires 1 pass; more passes are allowed and ignored.
-
laxCheckpoint
public Checkpoint laxCheckpoint(int requiredNumberOfPasses)
Create a lax checkpoint.- Parameters:
requiredNumberOfPasses- the required number of passes to validate the checkpoint.- Returns:
- a checkpoint that requires several passes; more passes than the required number are allowed and ignored.
-
checkpoint
public Checkpoint checkpoint()
Create a strict checkpoint.- Returns:
- a checkpoint that requires 1 pass, and makes the context fail if it is called more than once.
-
checkpoint
public Checkpoint checkpoint(int requiredNumberOfPasses)
Create a strict checkpoint.- Parameters:
requiredNumberOfPasses- the required number of passes to validate the checkpoint.- Returns:
- a checkpoint that requires several passes, but no more, or it fails the context.
-
succeeding
public <T> Handler<AsyncResult<T>> succeeding(Handler<T> nextHandler)
Create an asynchronous result handler that expects a success, and passes the value to another handler.- Type Parameters:
T- the asynchronous result type.- Parameters:
nextHandler- the value handler to call on success that is expected not to throw aThrowable.- Returns:
- the handler.
-
failing
@Deprecated public <T> Handler<AsyncResult<T>> failing()
Deprecated.UsefailingThenComplete()orfailing(Handler), for examplefailing(e -> checkpoint.flag()),failing(e -> { more testing code }), orfailing(e -> {}).Create an asynchronous result handler that expects a failure.- Type Parameters:
T- the asynchronous result type.- Returns:
- the handler.
-
failing
public <T> Handler<AsyncResult<T>> failing(Handler<Throwable> nextHandler)
Create an asynchronous result handler that expects a failure, and passes the exception to another handler.- Type Parameters:
T- the asynchronous result type.- Parameters:
nextHandler- the exception handler to call on failure that is expected not to throw aThrowable.- Returns:
- the handler.
-
succeedingThenComplete
public <T> Handler<AsyncResult<T>> succeedingThenComplete()
Create an asynchronous result handler that expects a success to then complete the test context.- Type Parameters:
T- the asynchronous result type.- Returns:
- the handler.
-
completing
@Deprecated public <T> Handler<AsyncResult<T>> completing()
Deprecated.UsesucceedingThenComplete()instead.Create an asynchronous result handler that expects a success to then complete the test context.- Type Parameters:
T- the asynchronous result type.- Returns:
- the handler.
- See Also:
failingThenComplete()
-
failingThenComplete
public <T> Handler<AsyncResult<T>> failingThenComplete()
Create an asynchronous result handler that expects a failure to then complete the test context.- Type Parameters:
T- the asynchronous result type.- Returns:
- the handler.
-
assertComplete
public <T> Future<T> assertComplete(Future<T> fut)
This method allows you to check if a future is completed. It internally creates a checkpoint. You can use it in a chain of `Future`.- Parameters:
fut- The future to assert success- Returns:
- a future with completion result
-
assertFailure
public <T> Future<T> assertFailure(Future<T> fut)
This method allows you to check if a future is failed. It internally creates a checkpoint. You can use it in a chain of `Future`.- Parameters:
fut- The future to assert failure- Returns:
- a future with failure result
-
verify
public VertxTestContext verify(VertxTestContext.ExecutionBlock block)
Allow verifications and assertions to be made.This method allows any assertion API to be used. The semantic is that the verification is successful when no exception is being thrown upon calling
block, otherwise the context fails with that exception.- Parameters:
block- a block of code to execute.- Returns:
- this context.
-
awaitCompletion
public boolean awaitCompletion(long timeout, TimeUnit unit) throws InterruptedExceptionWait for the completion of the test context.This method is automatically called by the
VertxExtensionwhen using parameter injection ofVertxTestContext. You should only call it when you instantiate this class manually.- Parameters:
timeout- the timeout.unit- the timeout unit.- Returns:
trueif the completion or failure happens before the timeout has been reached,falseotherwise.- Throws:
InterruptedException- when the thread has been interrupted.
-
-