testing with StepVerifier (#5673)
* testing with StepVerifier * Test publisher
This commit is contained in:
parent
9c9e32aa7c
commit
743949afe6
|
@ -143,7 +143,7 @@
|
|||
<jsonb-api.version>1.0</jsonb-api.version>
|
||||
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<project-reactor-test>3.1.6.RELEASE</project-reactor-test>
|
||||
<project-reactor-test>3.2.3.RELEASE</project-reactor-test>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.stepverifier;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
public class PostExecutionUnitTest {
|
||||
|
||||
Flux<Integer> source = Flux.<Integer>create(emitter -> {
|
||||
emitter.next(1);
|
||||
emitter.next(2);
|
||||
emitter.next(3);
|
||||
emitter.complete();
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
emitter.next(4);
|
||||
}).filter(number -> number % 2 == 0);
|
||||
|
||||
@Test
|
||||
public void droppedElements() {
|
||||
StepVerifier.create(source)
|
||||
.expectNext(2)
|
||||
.expectComplete()
|
||||
.verifyThenAssertThat()
|
||||
.hasDropped(4)
|
||||
.tookLessThan(Duration.ofMillis(1050));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.stepverifier;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
public class StepByStepUnitTest {
|
||||
|
||||
Flux<String> source = Flux.just("John", "Monica", "Mark", "Cloe", "Frank", "Casper", "Olivia", "Emily", "Cate")
|
||||
.filter(name -> name.length() == 4)
|
||||
.map(String::toUpperCase);
|
||||
|
||||
@Test
|
||||
public void shouldReturnForLettersUpperCaseStrings() {
|
||||
StepVerifier
|
||||
.create(source)
|
||||
.expectNext("JOHN")
|
||||
.expectNextMatches(name -> name.startsWith("MA"))
|
||||
.expectNext("CLOE", "CATE")
|
||||
.expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowExceptionAfterFourElements() {
|
||||
Flux<String> error = source.concatWith(
|
||||
Mono.error(new IllegalArgumentException("Our message"))
|
||||
);
|
||||
|
||||
StepVerifier
|
||||
.create(error)
|
||||
.expectNextCount(4)
|
||||
.expectErrorMatches(throwable -> throwable instanceof IllegalArgumentException &&
|
||||
throwable.getMessage().equals("Our message")
|
||||
).verify();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.stepverifier;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.StepVerifier;
|
||||
import reactor.test.publisher.TestPublisher;
|
||||
|
||||
public class TestingTestPublisherUnitTest {
|
||||
|
||||
@Test
|
||||
public void testPublisher() {
|
||||
TestPublisher
|
||||
.<String>create()
|
||||
.next("First", "Second", "Third")
|
||||
.error(new RuntimeException("Message"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonCompliant() {
|
||||
TestPublisher
|
||||
.createNoncompliant(TestPublisher.Violation.ALLOW_NULL)
|
||||
.emit("1", "2", null, "3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPublisherInAction() {
|
||||
final TestPublisher<String> testPublisher = TestPublisher.create();
|
||||
|
||||
UppercaseConverter uppercaseConverter = new UppercaseConverter(testPublisher.flux());
|
||||
|
||||
StepVerifier.create(uppercaseConverter.getUpperCase())
|
||||
.then(() -> testPublisher.emit("aA", "bb", "ccc"))
|
||||
.expectNext("AA", "BB", "CCC")
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class UppercaseConverter {
|
||||
private final Flux<String> source;
|
||||
|
||||
UppercaseConverter(Flux<String> source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
Flux<String> getUpperCase() {
|
||||
return source
|
||||
.map(String::toUpperCase);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.stepverifier;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
public class TimeBasedUnitTest {
|
||||
|
||||
@Test
|
||||
public void simpleExample() {
|
||||
StepVerifier
|
||||
.withVirtualTime(() -> Flux.interval(Duration.ofSeconds(1)).take(2))
|
||||
.expectSubscription()
|
||||
.expectNoEvent(Duration.ofSeconds(1))
|
||||
.expectNext(0L)
|
||||
.thenAwait(Duration.ofSeconds(1))
|
||||
.expectNext(1L)
|
||||
.verifyComplete();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue