From c7b2aded99c6e212c2b9b2b624873ba4fd4e4a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Mattos=20Torr=C3=A3o?= Date: Sun, 3 Jun 2018 03:47:42 -0300 Subject: [PATCH] BAEL-1325 - Spring Data Reactive Repositories with MongoDB (#4373) * refactor: use StepVerifier * refactor: use test observer --- spring-5-reactive/pom.xml | 7 ++++ .../AccountCrudRepositoryIntegrationTest.java | 37 ++++++++++++++----- ...AccountMongoRepositoryIntegrationTest.java | 35 ++++++++++++------ ...ccountRxJavaRepositoryIntegrationTest.java | 35 +++++++++++++----- 4 files changed, 84 insertions(+), 30 deletions(-) diff --git a/spring-5-reactive/pom.xml b/spring-5-reactive/pom.xml index 9c317e9966..8b40ccee00 100644 --- a/spring-5-reactive/pom.xml +++ b/spring-5-reactive/pom.xml @@ -141,6 +141,12 @@ rxjava ${rxjava-version} + + io.projectreactor + reactor-test + ${project-reactor-test} + test + @@ -185,6 +191,7 @@ 1.0 1.0 4.1 + 3.1.6.RELEASE diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java index 86f995ed79..f425826dce 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java @@ -10,6 +10,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -25,26 +26,44 @@ public class AccountCrudRepositoryIntegrationTest { public void givenValue_whenFindAllByValue_thenFindAccount() { repository.save(new Account(null, "Bill", 12.3)).block(); Flux accountFlux = repository.findAllByValue(12.3); - Account account = accountFlux.next().block(); - assertEquals("Bill", account.getOwner()); - assertEquals(Double.valueOf(12.3) , account.getValue()); - assertNotNull(account.getId()); + + StepVerifier.create(accountFlux) + .assertNext(account -> { + assertEquals("Bill", account.getOwner()); + assertEquals(Double.valueOf(12.3) , account.getValue()); + assertNotNull(account.getId()); + }) + .expectComplete() + .verify(); } @Test public void givenOwner_whenFindFirstByOwner_thenFindAccount() { repository.save(new Account(null, "Bill", 12.3)).block(); Mono accountMono = repository.findFirstByOwner(Mono.just("Bill")); - Account account = accountMono.block(); - assertEquals("Bill", account.getOwner()); - assertEquals(Double.valueOf(12.3) , account.getValue()); - assertNotNull(account.getId()); + + StepVerifier.create(accountMono) + .assertNext(account -> { + assertEquals("Bill", account.getOwner()); + assertEquals(Double.valueOf(12.3) , account.getValue()); + assertNotNull(account.getId()); + }) + .expectComplete() + .verify(); + + + } @Test public void givenAccount_whenSave_thenSaveAccount() { Mono accountMono = repository.save(new Account(null, "Bill", 12.3)); - assertNotNull(accountMono.block().getId()); + + StepVerifier + .create(accountMono) + .assertNext(account -> assertNotNull(account.getId())) + .expectComplete() + .verify(); } diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java index f95c443b7f..bfa6a789b2 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java @@ -2,8 +2,6 @@ package com.baeldung.reactive.repository; import com.baeldung.reactive.Spring5ReactiveApplication; import com.baeldung.reactive.model.Account; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -13,10 +11,10 @@ import org.springframework.data.domain.ExampleMatcher; import org.springframework.test.context.junit4.SpringRunner; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; -import java.util.List; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith; @RunWith(SpringRunner.class) @@ -32,23 +30,38 @@ public class AccountMongoRepositoryIntegrationTest { ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("owner", startsWith()); Example example = Example.of(new Account(null, "jo", null), matcher); Flux accountFlux = repository.findAll(example); - List accounts = accountFlux.collectList().block(); - assertTrue(accounts.stream().anyMatch(x -> x.getOwner().equals("john"))); + StepVerifier + .create(accountFlux) + .assertNext(account -> assertEquals("john", account.getOwner())) + .expectComplete() + .verify(); } @Test public void givenAccount_whenSave_thenSave() { Mono accountMono = repository.save(new Account(null, "john", 12.3)); - assertNotNull(accountMono.block().getId()); + + StepVerifier + .create(accountMono) + .assertNext(account -> assertNotNull(account.getId())) + .expectComplete() + .verify(); } @Test public void givenId_whenFindById_thenFindAccount() { Account inserted = repository.save(new Account(null, "john", 12.3)).block(); Mono accountMono = repository.findById(inserted.getId()); - assertEquals("john", accountMono.block().getOwner()); - assertEquals(Double.valueOf(12.3), accountMono.block().getValue()); - assertNotNull(accountMono.block().getId()); + + StepVerifier + .create(accountMono) + .assertNext(account -> { + assertEquals("john", account.getOwner()); + assertEquals(Double.valueOf(12.3), account.getValue()); + assertNotNull(account.getId()); + }) + .expectComplete() + .verify(); } } \ No newline at end of file diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java index 6199b460d0..e9b3eb1c40 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java @@ -21,23 +21,38 @@ public class AccountRxJavaRepositoryIntegrationTest { AccountRxJavaRepository repository; @Test - public void givenValue_whenFindAllByValue_thenFindAccounts() { + public void givenValue_whenFindAllByValue_thenFindAccounts() throws InterruptedException { repository.save(new Account(null, "bruno", 12.3)).blockingGet(); Observable accountObservable = repository.findAllByValue(12.3); - Account account = accountObservable.filter(x -> x.getOwner().equals("bruno")).blockingFirst(); - assertEquals("bruno", account.getOwner()); - assertEquals(Double.valueOf(12.3), account.getValue()); - assertNotNull(account.getId()); + + accountObservable + .test() + .await() + .assertComplete() + .assertValueAt(0, account -> { + assertEquals("bruno", account.getOwner()); + assertEquals(Double.valueOf(12.3), account.getValue()); + return true; + }); + } @Test - public void givenOwner_whenFindFirstByOwner_thenFindAccount() { + public void givenOwner_whenFindFirstByOwner_thenFindAccount() throws InterruptedException { repository.save(new Account(null, "bruno", 12.3)).blockingGet(); Single accountSingle = repository.findFirstByOwner(Single.just("bruno")); - Account account = accountSingle.blockingGet(); - assertEquals("bruno", account.getOwner()); - assertEquals(Double.valueOf(12.3), account.getValue()); - assertNotNull(account.getId()); + + accountSingle + .test() + .await() + .assertComplete() + .assertValueAt(0, account -> { + assertEquals("bruno", account.getOwner()); + assertEquals(Double.valueOf(12.3), account.getValue()); + assertNotNull(account.getId()); + return true; + }); + } } \ No newline at end of file