Unit testing is a software testing method by which individual units of source code are tested to determine whether they are fit for use.
StepVerifier is an instrument for testing reactive streams. It provides a declarative way of creating a verifiable script for an async Publisher sequence, by expressing expectations about the events that will happen upon subscription. The verification must be triggered after the terminal expectations (completion, error, cancellation) have been declared, by calling one of the verify() methods.
In short, StepVerifier is an instrument that the developers can use to tests reactive streams.
- Create a StepVerifier for a Publisher: use create(Publisher) or withVirtualTime(Supplier)
- Set up individual value expectations: use expectNext, expectNextMatches(Predicate), assertNext(Consumer), expectNextCount(long) or expectNextSequence(Iterable).
- Trigger subscription actions during the verification: use thenRequest(long) or thenCancel().
- Finalize the test scenario: use expectComplete(), expectError(), expectError(Class), expectErrorMatches(Predicate), or thenCancel().
- Trigger the verification of the resulting StepVerifier on its Publisher: use either verify() or verify(Duration).
- If any expectations failed, an AssertionError will be thrown indicating the failures.
In this example, we will look at some of the methods listed above
We need a testCompile dependency in order to be able to use StepVerifier in our tests:
testCompile(group = "io.projectreactor", name = "reactor-test", version = "3.2.3.RELEASE")
For this example, we have created a dummy model class that will be used in our tests:
data class Person(
val firstName: String,
val lastName: String
)