66import static org .hamcrest .collection .IsIterableContainingInOrder .contains ;
77import static org .hamcrest .core .IsNot .not ;
88
9- import java .util .ArrayList ;
10- import java .util .List ;
11-
9+ import org .baeldung .persistence .dao .MyUserPredicatesBuilder ;
10+ import org .baeldung .persistence .dao .MyUserRepository ;
1211import org .baeldung .persistence .model .MyUser ;
13- import org .baeldung .persistence .service .impl .MyUserService ;
1412import org .baeldung .spring .PersistenceConfig ;
15- import org .baeldung .web .util .SearchCriteria ;
1613import org .junit .Before ;
1714import org .junit .Test ;
1815import org .junit .runner .RunWith ;
2926public class JPAQuerydslTest {
3027
3128 @ Autowired
32- private MyUserService service ;
29+ private MyUserRepository repo ;
3330
3431 private MyUser userJohn ;
3532
@@ -42,65 +39,57 @@ public void init() {
4239 userJohn .setLastName ("Doe" );
4340 userJohn .setEmail ("john@doe.com" );
4441 userJohn .setAge (22 );
45- service .save (userJohn );
42+ repo .save (userJohn );
4643
4744 userTom = new MyUser ();
4845 userTom .setFirstName ("Tom" );
4946 userTom .setLastName ("Doe" );
5047 userTom .setEmail ("tom@doe.com" );
5148 userTom .setAge (26 );
52- service .save (userTom );
49+ repo .save (userTom );
5350 }
5451
5552 @ Test
5653 public void givenLast_whenGettingListOfUsers_thenCorrect () {
57- final List <SearchCriteria > params = new ArrayList <SearchCriteria >();
58- params .add (new SearchCriteria ("lastName" , ":" , "Doe" ));
54+ final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder ().with ("lastName" , ":" , "Doe" );
5955
60- final Iterable <MyUser > results = service . search ( params );
56+ final Iterable <MyUser > results = repo . findAll ( builder . build () );
6157 assertThat (results , containsInAnyOrder (userJohn , userTom ));
6258 }
6359
6460 @ Test
6561 public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect () {
66- final List <SearchCriteria > params = new ArrayList <SearchCriteria >();
67- params .add (new SearchCriteria ("firstName" , ":" , "John" ));
68- params .add (new SearchCriteria ("lastName" , ":" , "Doe" ));
62+ final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder ().with ("firstName" , ":" , "John" ).with ("lastName" , ":" , "Doe" );
6963
70- final Iterable <MyUser > results = service . search ( params );
64+ final Iterable <MyUser > results = repo . findAll ( builder . build () );
7165
7266 assertThat (results , contains (userJohn ));
7367 assertThat (results , not (contains (userTom )));
7468 }
7569
7670 @ Test
7771 public void givenLastAndAge_whenGettingListOfUsers_thenCorrect () {
78- final List <SearchCriteria > params = new ArrayList <SearchCriteria >();
79- params .add (new SearchCriteria ("lastName" , ":" , "Doe" ));
80- params .add (new SearchCriteria ("age" , ">" , "25" ));
72+ final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder ().with ("lastName" , ":" , "Doe" ).with ("age" , ">" , "25" );
8173
82- final Iterable <MyUser > results = service . search ( params );
74+ final Iterable <MyUser > results = repo . findAll ( builder . build () );
8375
8476 assertThat (results , contains (userTom ));
8577 assertThat (results , not (contains (userJohn )));
8678 }
8779
8880 @ Test
8981 public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect () {
90- final List <SearchCriteria > params = new ArrayList <SearchCriteria >();
91- params .add (new SearchCriteria ("firstName" , ":" , "Adam" ));
92- params .add (new SearchCriteria ("lastName" , ":" , "Fox" ));
82+ final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder ().with ("firstName" , ":" , "Adam" ).with ("lastName" , ":" , "Fox" );
9383
94- final Iterable <MyUser > results = service . search ( params );
84+ final Iterable <MyUser > results = repo . findAll ( builder . build () );
9585 assertThat (results , emptyIterable ());
9686 }
9787
9888 @ Test
9989 public void givenPartialFirst_whenGettingListOfUsers_thenCorrect () {
100- final List <SearchCriteria > params = new ArrayList <SearchCriteria >();
101- params .add (new SearchCriteria ("firstName" , ":" , "jo" ));
90+ final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder ().with ("firstName" , ":" , "jo" );
10291
103- final Iterable <MyUser > results = service . search ( params );
92+ final Iterable <MyUser > results = repo . findAll ( builder . build () );
10493
10594 assertThat (results , contains (userJohn ));
10695 assertThat (results , not (contains (userTom )));
0 commit comments