Skip to content

Commit c7b2ade

Browse files
Bruno Mattos Torrãopedja4
authored andcommitted
BAEL-1325 - Spring Data Reactive Repositories with MongoDB (eugenp#4373)
* refactor: use StepVerifier * refactor: use test observer
1 parent 3f9cf77 commit c7b2ade

4 files changed

Lines changed: 84 additions & 30 deletions

File tree

‎spring-5-reactive/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@
141141
<artifactId>rxjava</artifactId>
142142
<version>${rxjava-version}</version>
143143
</dependency>
144+
<dependency>
145+
<groupId>io.projectreactor</groupId>
146+
<artifactId>reactor-test</artifactId>
147+
<version>${project-reactor-test}</version>
148+
<scope>test</scope>
149+
</dependency>
144150

145151
</dependencies>
146152

@@ -185,6 +191,7 @@
185191
<jsonb-api.version>1.0</jsonb-api.version>
186192
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
187193
<commons-collections4.version>4.1</commons-collections4.version>
194+
<project-reactor-test>3.1.6.RELEASE</project-reactor-test>
188195
</properties>
189196

190197
</project>

‎spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountCrudRepositoryIntegrationTest.java‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.test.context.junit4.SpringRunner;
1111
import reactor.core.publisher.Flux;
1212
import reactor.core.publisher.Mono;
13+
import reactor.test.StepVerifier;
1314

1415
import static org.junit.Assert.assertEquals;
1516
import static org.junit.Assert.assertNotNull;
@@ -25,26 +26,44 @@ public class AccountCrudRepositoryIntegrationTest {
2526
public void givenValue_whenFindAllByValue_thenFindAccount() {
2627
repository.save(new Account(null, "Bill", 12.3)).block();
2728
Flux<Account> accountFlux = repository.findAllByValue(12.3);
28-
Account account = accountFlux.next().block();
29-
assertEquals("Bill", account.getOwner());
30-
assertEquals(Double.valueOf(12.3) , account.getValue());
31-
assertNotNull(account.getId());
29+
30+
StepVerifier.create(accountFlux)
31+
.assertNext(account -> {
32+
assertEquals("Bill", account.getOwner());
33+
assertEquals(Double.valueOf(12.3) , account.getValue());
34+
assertNotNull(account.getId());
35+
})
36+
.expectComplete()
37+
.verify();
3238
}
3339

3440
@Test
3541
public void givenOwner_whenFindFirstByOwner_thenFindAccount() {
3642
repository.save(new Account(null, "Bill", 12.3)).block();
3743
Mono<Account> accountMono = repository.findFirstByOwner(Mono.just("Bill"));
38-
Account account = accountMono.block();
39-
assertEquals("Bill", account.getOwner());
40-
assertEquals(Double.valueOf(12.3) , account.getValue());
41-
assertNotNull(account.getId());
44+
45+
StepVerifier.create(accountMono)
46+
.assertNext(account -> {
47+
assertEquals("Bill", account.getOwner());
48+
assertEquals(Double.valueOf(12.3) , account.getValue());
49+
assertNotNull(account.getId());
50+
})
51+
.expectComplete()
52+
.verify();
53+
54+
55+
4256
}
4357

4458
@Test
4559
public void givenAccount_whenSave_thenSaveAccount() {
4660
Mono<Account> accountMono = repository.save(new Account(null, "Bill", 12.3));
47-
assertNotNull(accountMono.block().getId());
61+
62+
StepVerifier
63+
.create(accountMono)
64+
.assertNext(account -> assertNotNull(account.getId()))
65+
.expectComplete()
66+
.verify();
4867
}
4968

5069

‎spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountMongoRepositoryIntegrationTest.java‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.baeldung.reactive.Spring5ReactiveApplication;
44
import com.baeldung.reactive.model.Account;
5-
import org.junit.After;
6-
import org.junit.Before;
75
import org.junit.Test;
86
import org.junit.runner.RunWith;
97
import org.springframework.beans.factory.annotation.Autowired;
@@ -13,10 +11,10 @@
1311
import org.springframework.test.context.junit4.SpringRunner;
1412
import reactor.core.publisher.Flux;
1513
import reactor.core.publisher.Mono;
14+
import reactor.test.StepVerifier;
1615

17-
import java.util.List;
18-
19-
import static org.junit.Assert.*;
16+
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertNotNull;
2018
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith;
2119

2220
@RunWith(SpringRunner.class)
@@ -32,23 +30,38 @@ public void givenExample_whenFindAllWithExample_thenFindAllMacthings() {
3230
ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("owner", startsWith());
3331
Example<Account> example = Example.of(new Account(null, "jo", null), matcher);
3432
Flux<Account> accountFlux = repository.findAll(example);
35-
List<Account> accounts = accountFlux.collectList().block();
3633

37-
assertTrue(accounts.stream().anyMatch(x -> x.getOwner().equals("john")));
34+
StepVerifier
35+
.create(accountFlux)
36+
.assertNext(account -> assertEquals("john", account.getOwner()))
37+
.expectComplete()
38+
.verify();
3839
}
3940

4041
@Test
4142
public void givenAccount_whenSave_thenSave() {
4243
Mono<Account> accountMono = repository.save(new Account(null, "john", 12.3));
43-
assertNotNull(accountMono.block().getId());
44+
45+
StepVerifier
46+
.create(accountMono)
47+
.assertNext(account -> assertNotNull(account.getId()))
48+
.expectComplete()
49+
.verify();
4450
}
4551

4652
@Test
4753
public void givenId_whenFindById_thenFindAccount() {
4854
Account inserted = repository.save(new Account(null, "john", 12.3)).block();
4955
Mono<Account> accountMono = repository.findById(inserted.getId());
50-
assertEquals("john", accountMono.block().getOwner());
51-
assertEquals(Double.valueOf(12.3), accountMono.block().getValue());
52-
assertNotNull(accountMono.block().getId());
56+
57+
StepVerifier
58+
.create(accountMono)
59+
.assertNext(account -> {
60+
assertEquals("john", account.getOwner());
61+
assertEquals(Double.valueOf(12.3), account.getValue());
62+
assertNotNull(account.getId());
63+
})
64+
.expectComplete()
65+
.verify();
5366
}
5467
}

‎spring-5-reactive/src/test/java/com/baeldung/reactive/repository/AccountRxJavaRepositoryIntegrationTest.java‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,38 @@ public class AccountRxJavaRepositoryIntegrationTest {
2121
AccountRxJavaRepository repository;
2222

2323
@Test
24-
public void givenValue_whenFindAllByValue_thenFindAccounts() {
24+
public void givenValue_whenFindAllByValue_thenFindAccounts() throws InterruptedException {
2525
repository.save(new Account(null, "bruno", 12.3)).blockingGet();
2626
Observable<Account> accountObservable = repository.findAllByValue(12.3);
27-
Account account = accountObservable.filter(x -> x.getOwner().equals("bruno")).blockingFirst();
28-
assertEquals("bruno", account.getOwner());
29-
assertEquals(Double.valueOf(12.3), account.getValue());
30-
assertNotNull(account.getId());
27+
28+
accountObservable
29+
.test()
30+
.await()
31+
.assertComplete()
32+
.assertValueAt(0, account -> {
33+
assertEquals("bruno", account.getOwner());
34+
assertEquals(Double.valueOf(12.3), account.getValue());
35+
return true;
36+
});
37+
3138
}
3239

3340
@Test
34-
public void givenOwner_whenFindFirstByOwner_thenFindAccount() {
41+
public void givenOwner_whenFindFirstByOwner_thenFindAccount() throws InterruptedException {
3542
repository.save(new Account(null, "bruno", 12.3)).blockingGet();
3643
Single<Account> accountSingle = repository.findFirstByOwner(Single.just("bruno"));
37-
Account account = accountSingle.blockingGet();
38-
assertEquals("bruno", account.getOwner());
39-
assertEquals(Double.valueOf(12.3), account.getValue());
40-
assertNotNull(account.getId());
44+
45+
accountSingle
46+
.test()
47+
.await()
48+
.assertComplete()
49+
.assertValueAt(0, account -> {
50+
assertEquals("bruno", account.getOwner());
51+
assertEquals(Double.valueOf(12.3), account.getValue());
52+
assertNotNull(account.getId());
53+
return true;
54+
});
55+
4156
}
4257

4358
}

0 commit comments

Comments
 (0)