|
| 1 | +package org.baeldung.persistence.query; |
| 2 | + |
| 3 | +import static org.baeldung.persistence.dao.UserSpecifications.ageIsGreaterThan; |
| 4 | +import static org.baeldung.persistence.dao.UserSpecifications.firstNameIsLike; |
| 5 | +import static org.baeldung.persistence.dao.UserSpecifications.lastNameIsLike; |
| 6 | +import static org.junit.Assert.assertEquals; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.baeldung.config.PersistenceJPAConfig; |
| 11 | +import org.baeldung.persistence.dao.UserRepository; |
| 12 | +import org.baeldung.persistence.model.User; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.data.jpa.domain.Specifications; |
| 18 | +import org.springframework.test.context.ContextConfiguration; |
| 19 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 20 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 21 | +import org.springframework.test.context.transaction.TransactionConfiguration; |
| 22 | +import org.springframework.transaction.annotation.Transactional; |
| 23 | + |
| 24 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 25 | +@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) |
| 26 | +@Transactional |
| 27 | +@TransactionConfiguration |
| 28 | +public class JPASpecificationsTest { |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private UserRepository repository; |
| 32 | + |
| 33 | + private User userJohn; |
| 34 | + |
| 35 | + private User userTom; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void init() { |
| 39 | + userJohn = new User(); |
| 40 | + userJohn.setFirstName("John"); |
| 41 | + userJohn.setLastName("Doe"); |
| 42 | + userJohn.setEmail("john@doe.com"); |
| 43 | + userJohn.setAge(22); |
| 44 | + repository.save(userJohn); |
| 45 | + |
| 46 | + userTom = new User(); |
| 47 | + userTom.setFirstName("Tom"); |
| 48 | + userTom.setLastName("Doe"); |
| 49 | + userTom.setEmail("tom@doe.com"); |
| 50 | + userTom.setAge(26); |
| 51 | + repository.save(userTom); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenLast_whenGettingListOfUsers_thenCorrect() { |
| 56 | + final List<User> result = repository.findAll(lastNameIsLike("doe")); |
| 57 | + assertEquals(2, result.size()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { |
| 62 | + final List<User> result = repository.findAll(Specifications.where(lastNameIsLike("doe")).and(firstNameIsLike("john"))); |
| 63 | + |
| 64 | + assertEquals(1, result.size()); |
| 65 | + assertEquals(userJohn.getEmail(), result.get(0).getEmail()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() { |
| 70 | + final List<User> result = repository.findAll(Specifications.where(lastNameIsLike("doe")).and(ageIsGreaterThan(25))); |
| 71 | + |
| 72 | + assertEquals(1, result.size()); |
| 73 | + assertEquals(userTom.getEmail(), result.get(0).getEmail()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() { |
| 78 | + final List<User> result = repository.findAll(Specifications.where(lastNameIsLike("Adam")).and(firstNameIsLike("Fox"))); |
| 79 | + assertEquals(0, result.size()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() { |
| 84 | + final List<User> result = repository.findAll(firstNameIsLike("jo")); |
| 85 | + |
| 86 | + assertEquals(1, result.size()); |
| 87 | + assertEquals(userJohn.getEmail(), result.get(0).getEmail()); |
| 88 | + } |
| 89 | +} |
0 commit comments