Exercises
In these exercises, you’ll practice some of the concepts taught in this module.
First, either create a new Java project, adding the reactor-bom
, reactor-test
, and reactor-core
dependencies to your build file (Maven or Gradle) or use the stub you can find at: https://github.com/eh3rrera/project-reactor-course/tree/main/09/before/reactor-demo-exercises.
I’ll give you the instructions (and sometimes hints) so you can put all the code together in a method of a test class and make the test pass.
Here you can find the solution for the coding exercises: https://github.com/eh3rrera/project-reactor-course/tree/main/09/after/reactor-demo-exercises.
Exercise 1
In this exercise, you’ll use StepVerifier
interface to test a Publisher
that doesn’t terminate but rather times out after the provided duration.
- Create a unit test class named
Test01
with a method marked with the@Test
annotation. - Create a
Mono
that emits an integer value after3
seconds. - Use the
StepVerifier
to create a test scenario with the StepVerifier.LastStep.expectTimeout(Duration) method to verify that theMono
times out after2
seconds. - Run the test scenario and verify the results. The test should pass.
Exercise 2
In this coding exercise, you’ll use the StepVerifier
interface to test if the next element emitted by a Publisher
matches the given predicate.
- Create a unit test class named
Test02
with a method marked with the@Test
annotation. - Create a
Flux
that emits a sequence of integers from1
to3
. - Define a predicate that checks if a number is even.
- Use StepVerifier to create a test scenario using the StepVerifier.Step.expectNext(T) and StepVerifier.Step.expectNextMatches(Predicate) methods.
- Run the test scenario and verify the results. The test should pass.
Exercise 3
In this exercise, you’ll use the StepVerifier
interface to check if the error matches the given predicate.
- Create a unit test class named
Test03
with a method marked with the@Test
annotation. - Create a
Flux
that emits a sequence of integers from1
to3
and concatenate it with anotherFlux
that throws anIllegalStateException
immediately after subscription. - Define a
Predicate
that checks if the error is an instance ofIllegalStateException
. - Use
StepVerifier
to create a test scenario for theFlux
, expecting the sequence of integers, and then the exception. For the latter, use the StepVerifier.LastStep.verifyErrorMatches(Predicate) method. - Run the test scenario and verify the results. The test should pass.
Exercise 4
In this exercise, you’ll use the StepVerifier
interface to test a Publisher
by using the StepVerifier.verifyThenAssertThat() method to verify the signals received and then assert the final state.
- Create a unit test class named
Test04
with a method marked with the@Test
annotation. - Create a
Flux
that emits a sequence of integers from1
to3
. - Use
StepVerifier
to create a test scenario for theFlux
using the methodsexpectNext
andexpectComplete
. - Get an instance of
StepVerifier.Assertions
using theverifyThenAssertThat
method. - Finally, make the following assertions on the final state of the subscriber:
- No elements were dropped
- No errors were dropped
- No elements were discarded
- Run the test scenario and verify the results. The test should pass.
Exercise 5
In this exercise, you’ll use the StepVerifier
interface with VirtualTimeScheduler
to test a Publisher
with virtual time, allowing you to control the passage of time during the test.
- Create a unit test class named
Test05
with a method marked with the@Test
annotation. - Create a
Flux
that emits a sequence of integers from1
to3
with a delay of1
hour between each element. - Use the StepVerifier.withVirtualTime(Supplier) and StepVerifier.Step.thenAwait() methods to create a test scenario for the
Flux
. - Run the test scenario and verify the results. The test should pass.
Exercise 6
In this exercise, you’ll use PublisherProbe to test a Publisher
and verify that it was canceled at least once.
- Create a unit test class named
Test06
with a method marked with the@Test
annotation. - Create a
Flux
that emits a sequence of integers from1
to5
. - Create a
PublisherProbe
instance and use it to wrap theFlux
. - Use the
StepVerifier
to create a test scenario for the wrappedFlux
, canceling the subscription after receiving the first element with the methodthenCancel()
. - Run the test scenario.
- Use the
PublisherProbe
instance to assert that the probe was canceled. The test should pass.
Exercise 7
In this exercise, you’ll use TestPublisher to create a Publisher
for testing purposes, and use StepVerifier
to verify its behavior.
- Create a unit test class named
Test07
with a method marked with the@Test
annotation. - Create a cold
TestPublisher
instance for integer values. - Use the
TestPublisher
to emit a sequence from1
to3
using thenext(T, T...)
method. - Create a
Flux
from theTestPublisher
. - Use the
StepVerifier
to create a test scenario for theFlux
. Don’t forget to call thecomplete()
method on theTestPublisher
. - Run the test scenario and verify the results. The test should pass.