Add blocking and non-blocking mono examples

Add article reference

Fix url
This commit is contained in:
gindex 2020-03-28 19:01:54 +01:00
parent 1acadab18b
commit ba65e09989
2 changed files with 40 additions and 0 deletions

View File

@ -7,3 +7,4 @@ This module contains articles about Reactor Core.
- [Intro To Reactor Core](https://www.baeldung.com/reactor-core)
- [Combining Publishers in Project Reactor](https://www.baeldung.com/reactor-combine-streams)
- [Programmatically Creating Sequences with Project Reactor](https://www.baeldung.com/flux-sequences-reactor)
- [How To Get String From Mono In Reactive Java](http://baeldung.com/string-from-mono/)

View File

@ -0,0 +1,39 @@
package com.baeldung.mono;
import org.junit.Test;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
public class MonoUnitTest {
@Test
public void whenMonoProducesString_thenBlockAndConsume() {
String expected = "hello world!";
String result1 = Mono.just(expected).block();
assertEquals(expected, result1);
String result2 = Mono.just(expected).block(Duration.of(1000, ChronoUnit.MILLIS));
assertEquals(expected, result2);
Optional<String> result3 = Mono.<String>empty().blockOptional();
assertEquals(Optional.empty(), result3);
}
@Test
public void whenMonoProducesString_thenConsumeNonBlocking() {
String expected = "hello world!";
Mono.just(expected)
.doOnNext(result -> assertEquals(expected, result))
.subscribe();
Mono.just(expected)
.subscribe(result -> assertEquals(expected, result));
}
}