|
15 | 15 | */
|
16 | 16 | package example.springdata.jpa.java8;
|
17 | 17 |
|
18 |
| -import static org.hamcrest.CoreMatchers.*; |
| 18 | +import static org.hamcrest.Matchers.*; |
19 | 19 | import static org.junit.Assert.*;
|
20 | 20 |
|
21 |
| -import java.util.List; |
22 | 21 | import java.util.Optional;
|
23 | 22 | import java.util.stream.Collectors;
|
24 | 23 | import java.util.stream.Stream;
|
25 | 24 |
|
26 |
| -import org.hamcrest.core.IsCollectionContaining; |
27 | 25 | import org.junit.Test;
|
28 | 26 | import org.junit.runner.RunWith;
|
29 | 27 | import org.springframework.beans.factory.annotation.Autowired;
|
@@ -73,20 +71,32 @@ public void invokesDefaultMethod() {
|
73 | 71 | }
|
74 | 72 |
|
75 | 73 | /**
|
76 |
| - * Streaming data from the store by using a repsoitory method that returns a {@link Stream}. Note, that since the |
| 74 | + * Streaming data from the store by using a repository method that returns a {@link Stream}. Note, that since the |
77 | 75 | * resulting {@link Stream} contains state it needs to be closed explicitly after use!
|
78 | 76 | */
|
79 | 77 | @Test
|
80 |
| - public void useJava8StreamsDirectly() { |
| 78 | + public void useJava8StreamsWithCustomQuery() { |
81 | 79 |
|
82 | 80 | Customer customer1 = repository.save(new Customer("Customer1", "Foo"));
|
83 | 81 | Customer customer2 = repository.save(new Customer("Customer2", "Bar"));
|
84 | 82 |
|
85 | 83 | try (Stream<Customer> stream = repository.streamAllCustomers()) {
|
| 84 | + assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Streaming data from the store by using a repository method that returns a {@link Stream} with a derived query. |
| 90 | + * Note, that since the resulting {@link Stream} contains state it needs to be closed explicitly after use! |
| 91 | + */ |
| 92 | + @Test |
| 93 | + public void useJava8StreamsWithDerivedQuery() { |
86 | 94 |
|
87 |
| - List<Customer> customers = stream.collect(Collectors.toList()); |
| 95 | + Customer customer1 = repository.save(new Customer("Customer1", "Foo")); |
| 96 | + Customer customer2 = repository.save(new Customer("Customer2", "Bar")); |
88 | 97 |
|
89 |
| - assertThat(customers, IsCollectionContaining.<Customer> hasItems(customer1, customer2)); |
| 98 | + try (Stream<Customer> stream = repository.findAllByLastnameIsNotNull()) { |
| 99 | + assertThat(stream.collect(Collectors.toList()), hasItems(customer1, customer2)); |
90 | 100 | }
|
91 | 101 | }
|
92 | 102 | }
|
0 commit comments