Add blocking and non-blocking mono examples
Add article reference Fix url
This commit is contained in:
parent
1acadab18b
commit
ba65e09989
|
@ -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/)
|
|
@ -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));
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue