0
# Reactor Test
1
2
Reactor Test is a comprehensive testing support library for Project Reactor's reactive streams, providing utilities to verify the behavior of Publishers (Flux and Mono) through declarative APIs. It enables developers to create test scenarios that express expectations about events that occur upon subscription, including data emissions, errors, completion, and timing.
3
4
## Package Information
5
6
- **Package Name**: io.projectreactor:reactor-test
7
- **Package Type**: Maven
8
- **Language**: Java
9
- **Installation**: Add to Maven dependencies:
10
```xml
11
<dependency>
12
<groupId>io.projectreactor</groupId>
13
<artifactId>reactor-test</artifactId>
14
<version>3.7.6</version>
15
<scope>test</scope>
16
</dependency>
17
```
18
19
## Core Imports
20
21
```java
22
import reactor.test.StepVerifier;
23
import reactor.test.publisher.TestPublisher;
24
import reactor.test.subscriber.TestSubscriber;
25
import reactor.test.scheduler.VirtualTimeScheduler;
26
import reactor.test.publisher.PublisherProbe;
27
```
28
29
## Basic Usage
30
31
```java
32
import reactor.core.publisher.Flux;
33
import reactor.test.StepVerifier;
34
import java.time.Duration;
35
36
// Basic verification of a simple sequence
37
StepVerifier.create(Flux.just("foo", "bar"))
38
.expectNext("foo")
39
.expectNext("bar")
40
.expectComplete()
41
.verify();
42
43
// Virtual time testing for delayed sequences
44
StepVerifier.withVirtualTime(() -> Flux.just("hello").delayElements(Duration.ofMinutes(1)))
45
.expectSubscription()
46
.expectNoEvent(Duration.ofMinutes(1))
47
.expectNext("hello")
48
.expectComplete()
49
.verify();
50
```
51
52
## Architecture
53
54
Reactor Test provides four main testing approaches:
55
56
1. **Declarative Testing** - StepVerifier provides a fluent API for expressing step-by-step expectations
57
2. **Controlled Publishers** - TestPublisher allows manual control over signal emission
58
3. **Publisher Instrumentation** - PublisherProbe tracks subscription events for control flow verification
59
4. **Manual Subscribers** - TestSubscriber provides programmatic access to received signals for complex assertions
60
61
Additional utilities support virtual time manipulation, race condition testing, and log output verification.
62
63
## Capabilities
64
65
### Step-by-Step Verification
66
67
Verify reactive sequences declaratively by expressing expectations about events that will happen upon subscription.
68
69
```java { .api }
70
interface StepVerifier {
71
static <T> FirstStep<T> create(Publisher<? extends T> publisher);
72
static <T> FirstStep<T> withVirtualTime(Supplier<? extends Publisher<? extends T>> scenarioSupplier);
73
Duration verify();
74
Duration verify(Duration duration);
75
}
76
```
77
78
[Step-by-Step Verification](./step-verifier.md)
79
80
### Controlled Publishers
81
82
Create test publishers that can be manually controlled to emit specific signals for testing subscriber behavior.
83
84
```java { .api }
85
abstract class TestPublisher<T> implements Publisher<T> {
86
static <T> TestPublisher<T> create();
87
static <T> TestPublisher<T> createCold();
88
TestPublisher<T> next(T value);
89
TestPublisher<T> error(Throwable t);
90
TestPublisher<T> complete();
91
}
92
```
93
94
[Controlled Publishers](./test-publisher.md)
95
96
### Publisher Instrumentation
97
98
Instrument publishers to capture subscription events and verify control flow without affecting the actual data flow.
99
100
```java { .api }
101
interface PublisherProbe<T> {
102
static <T> PublisherProbe<T> of(Publisher<? extends T> source);
103
static <T> PublisherProbe<T> empty();
104
boolean wasSubscribed();
105
boolean wasCancelled();
106
void assertWasSubscribed();
107
}
108
```
109
110
[Publisher Instrumentation](./publisher-probe.md)
111
112
### Manual Subscribers
113
114
Create subscribers that collect signals programmatically for complex testing scenarios requiring more flexibility than StepVerifier.
115
116
```java { .api }
117
interface TestSubscriber<T> extends CoreSubscriber<T> {
118
static <T> TestSubscriber<T> create();
119
static TestSubscriberBuilder builder();
120
List<T> getReceivedOnNext();
121
boolean isTerminated();
122
void block(Duration timeout);
123
}
124
```
125
126
[Manual Subscribers](./test-subscriber.md)
127
128
### Virtual Time Control
129
130
Manipulate virtual time to test time-based reactive operations without real delays.
131
132
```java { .api }
133
class VirtualTimeScheduler implements Scheduler {
134
static VirtualTimeScheduler create();
135
static VirtualTimeScheduler getOrSet();
136
void advanceTimeBy(Duration delayTime);
137
void advanceTimeTo(Instant instant);
138
}
139
```
140
141
[Virtual Time Control](./virtual-time.md)
142
143
### Testing Utilities
144
145
Additional utilities for advanced testing scenarios including race condition testing and log output verification.
146
147
```java { .api }
148
class RaceTestUtils {
149
static void race(Runnable... rs);
150
static void race(Scheduler s, Runnable... rs);
151
}
152
153
class TestLogger implements Logger {
154
TestLogger();
155
String getOutContent();
156
String getErrContent();
157
}
158
```
159
160
[Testing Utilities](./testing-utilities.md)
161
162
## Types
163
164
```java { .api }
165
// Core configuration options
166
class StepVerifierOptions {
167
static StepVerifierOptions create();
168
StepVerifierOptions initialRequest(long initialRequest);
169
StepVerifierOptions virtualTimeSchedulerSupplier(Supplier<? extends VirtualTimeScheduler> vtsLookup);
170
}
171
172
// Test publisher violation modes
173
enum TestPublisher.Violation {
174
REQUEST_OVERFLOW, // Allow next calls despite insufficient request
175
ALLOW_NULL, // Allow null values in next calls
176
CLEANUP_ON_TERMINATE, // Allow multiple termination signals
177
DEFER_CANCELLATION // Ignore cancellation signals
178
}
179
180
// Fusion requirements for TestSubscriber
181
enum TestSubscriber.FusionRequirement {
182
FUSEABLE, // Expect publisher to be fuseable
183
NOT_FUSEABLE, // Expect publisher to not be fuseable
184
NONE // No fusion requirements
185
}
186
```