1717
1818import static org .assertj .core .api .Assertions .*;
1919
20+ import jakarta .persistence .EntityManager ;
21+
22+ import java .util .Collection ;
23+ import java .util .List ;
2024import java .util .Map ;
2125
2226import org .junit .jupiter .api .BeforeEach ;
2630import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
2731import org .springframework .boot .test .context .SpringBootTest ;
2832import org .springframework .context .annotation .Configuration ;
33+ import org .springframework .data .domain .Page ;
2934import org .springframework .data .domain .PageRequest ;
3035import org .springframework .data .domain .Sort ;
3136import org .springframework .data .domain .Sort .Direction ;
37+ import org .springframework .data .jpa .domain .PredicateSpecification ;
3238import org .springframework .data .projection .TargetAware ;
3339import org .springframework .transaction .annotation .Transactional ;
3440
@@ -48,6 +54,7 @@ class CustomerRepositoryIntegrationTest {
4854 static class Config {}
4955
5056 @ Autowired CustomerRepository customers ;
57+ @ Autowired EntityManager entityManager ;
5158
5259 private Customer dave ;
5360 private Customer carter ;
@@ -96,7 +103,7 @@ void projectsDynamically() {
96103 @ Test
97104 void projectsIndividualDynamically () {
98105
99- var result = customers .findProjectedById (dave .getId (), CustomerSummary .class );
106+ CustomerSummary result = customers .findProjectedById (dave .getId (), CustomerSummary .class );
100107
101108 assertThat (result .getFullName ()).isEqualTo ("Dave Matthews" );
102109
@@ -108,7 +115,7 @@ void projectsIndividualDynamically() {
108115 @ Test
109116 void projectIndividualInstance () {
110117
111- var projectedDave = customers .findProjectedById (dave .getId ());
118+ CustomerProjection projectedDave = customers .findProjectedById (dave .getId ());
112119
113120 assertThat (projectedDave .getFirstname ()).isEqualTo ("Dave" );
114121 assertThat (projectedDave ).isInstanceOfSatisfying (TargetAware .class ,
@@ -118,7 +125,7 @@ void projectIndividualInstance() {
118125 @ Test
119126 void projectsDtoUsingConstructorExpression () {
120127
121- var result = customers .findDtoWithConstructorExpression ("Dave" );
128+ Collection < CustomerDto > result = customers .findDtoWithConstructorExpression ("Dave" );
122129
123130 assertThat (result ).hasSize (1 );
124131 assertThat (result .iterator ().next ().firstname ()).isEqualTo ("Dave" );
@@ -127,7 +134,8 @@ void projectsDtoUsingConstructorExpression() {
127134 @ Test
128135 void supportsProjectionInCombinationWithPagination () {
129136
130- var page = customers .findPagedProjectedBy (PageRequest .of (0 , 1 , Sort .by (Direction .ASC , "lastname" )));
137+ Page <CustomerProjection > page = customers
138+ .findPagedProjectedBy (PageRequest .of (0 , 1 , Sort .by (Direction .ASC , "lastname" )));
131139
132140 assertThat (page .getContent ().get (0 ).getFirstname ()).isEqualTo ("Carter" );
133141 }
@@ -140,10 +148,13 @@ void appliesProjectionToOptional() {
140148 @ Test
141149 void projectsWithSpecification () {
142150
143- var result = customers .findBy ((query , cb ) -> cb .equal (query .get ("firstname" ), "Dave" ),
144- it -> it .as (CustomerDto .class ).all ());
151+ PredicateSpecification <Customer > isFirstName = (from , cb ) -> cb .equal (from .get ("firstname" ), "Dave" );
152+ PredicateSpecification <Customer > isLastName = (from , cb ) -> cb .equal (from .get ("lastname" ), "Matthews" );
153+
154+ List <CustomerDto > result = customers .findBy (isFirstName .and (isLastName ), it -> it .as (CustomerDto .class ).all ());
145155
146156 assertThat (result ).hasSize (1 );
147157 assertThat (result ).extracting (CustomerDto ::firstname ).containsOnly ("Dave" );
148158 }
159+
149160}
0 commit comments