diff --git a/reactor-core/README.md b/reactor-core/README.md index e3cca35f86..f2dbd77981 100644 --- a/reactor-core/README.md +++ b/reactor-core/README.md @@ -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/) \ No newline at end of file diff --git a/reactor-core/src/test/java/com/baeldung/mono/MonoUnitTest.java b/reactor-core/src/test/java/com/baeldung/mono/MonoUnitTest.java new file mode 100644 index 0000000000..b493cd1159 --- /dev/null +++ b/reactor-core/src/test/java/com/baeldung/mono/MonoUnitTest.java @@ -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 result3 = Mono.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)); + + } +}