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
Test01with a method marked with the@Testannotation. - Create a
Monothat emits an integer value after3seconds. - Use the
StepVerifierto create a test scenario with the StepVerifier.LastStep.expectTimeout(Duration) method to verify that theMonotimes out after2seconds. - 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
Test02with a method marked with the@Testannotation. - Create a
Fluxthat emits a sequence of integers from1to3. - 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
Test03with a method marked with the@Testannotation. - Create a
Fluxthat emits a sequence of integers from1to3and concatenate it with anotherFluxthat throws anIllegalStateExceptionimmediately after subscription. - Define a
Predicatethat checks if the error is an instance ofIllegalStateException. - Use
StepVerifierto 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
Test04with a method marked with the@Testannotation. - Create a
Fluxthat emits a sequence of integers from1to3. - Use
StepVerifierto create a test scenario for theFluxusing the methodsexpectNextandexpectComplete. - Get an instance of
StepVerifier.Assertionsusing theverifyThenAssertThatmethod. - 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
Test05with a method marked with the@Testannotation. - Create a
Fluxthat emits a sequence of integers from1to3with a delay of1hour 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
Test06with a method marked with the@Testannotation. - Create a
Fluxthat emits a sequence of integers from1to5. - Create a
PublisherProbeinstance and use it to wrap theFlux. - Use the
StepVerifierto create a test scenario for the wrappedFlux, canceling the subscription after receiving the first element with the methodthenCancel(). - Run the test scenario.
- Use the
PublisherProbeinstance 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
Test07with a method marked with the@Testannotation. - Create a cold
TestPublisherinstance for integer values. - Use the
TestPublisherto emit a sequence from1to3using thenext(T, T...)method. - Create a
Fluxfrom theTestPublisher. - Use the
StepVerifierto 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.