Skip to content

Commit 0a4ea41

Browse files
Add test for result streaming with data-jpa
See: spring-projects/spring-data-jpa#2848
1 parent 3fd2912 commit 0a4ea41

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

data/data-jpa/src/appTest/java/com/example/data/jpa/DataJpaApplicationAotTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ void queryAnnotatedMethod(AssertableOutput output) {
5656
});
5757
}
5858

59+
@Test
60+
void streamingResult(AssertableOutput output) {
61+
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
62+
assertThat(output).hasSingleLineContaining("streamAuthors(): author = Author{name='Martin Kleppmann'}")
63+
.hasNoLinesContaining("streamAuthors(): author = Author{name='Josh Long'}");
64+
});
65+
}
66+
5967
@Test
6068
void deleteAll(AssertableOutput output) {
6169
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {

data/data-jpa/src/main/java/com/example/data/jpa/AuthorRepository.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.data.jpa;
22

33
import java.util.Optional;
4+
import java.util.stream.Stream;
45

56
import com.example.data.jpa.model.Author;
67

@@ -14,4 +15,6 @@ public interface AuthorRepository extends ListCrudRepository<Author, Long> {
1415
@Query("SELECT a FROM Author a WHERE a.name = :name")
1516
Optional<Author> queryFindByName(String name);
1617

18+
Stream<Author> findByNameContaining(String name);
19+
1720
}

data/data-jpa/src/main/java/com/example/data/jpa/CLR.java

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Collections;
55
import java.util.List;
66
import java.util.Set;
7+
import java.util.stream.Stream;
78

89
import com.example.data.jpa.model.Author;
910
import com.example.data.jpa.model.Book;
@@ -32,6 +33,7 @@ public void run(String... args) {
3233
findById(authors);
3334
findByPartialName();
3435
queryFindByName();
36+
streamAuthors();
3537
deleteAll();
3638
entityGraph();
3739
}
@@ -87,6 +89,12 @@ private void listAllAuthors() {
8789
}
8890
}
8991

92+
private void streamAuthors() {
93+
94+
Stream<Author> authors = this.authorRepository.findByNameContaining("Martin");
95+
authors.forEach(author -> System.out.printf("streamAuthors(): author = %s%n", author));
96+
}
97+
9098
private List<Author> insertAuthors() {
9199
Author author1 = this.authorRepository.save(new Author(null, "Josh Long",
92100
Set.of(new Book(null, "Reactive Spring"), new Book(null, "Cloud Native Java"))));

0 commit comments

Comments
 (0)