|
6 | 6 | import java.util.Arrays;
|
7 | 7 | import java.util.List;
|
8 | 8 |
|
| 9 | +import com.baeldung.jpa.projection.view.AddressDto; |
9 | 10 | import org.junit.jupiter.api.Test;
|
10 | 11 | import org.springframework.beans.factory.annotation.Autowired;
|
11 | 12 | import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
|
12 | 13 | import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
13 | 14 | import org.springframework.test.context.jdbc.Sql;
|
14 |
| - |
15 | 15 | import com.baeldung.jpa.projection.model.Person;
|
16 | 16 | import com.baeldung.jpa.projection.repository.AddressRepository;
|
17 | 17 | import com.baeldung.jpa.projection.repository.PersonRepository;
|
@@ -40,6 +40,16 @@ void whenUsingClosedProjections_thenViewWithRequiredPropertiesIsReturned() {
|
40 | 40 | assertThat(personView.getLastName()).isEqualTo("Doe");
|
41 | 41 | }
|
42 | 42 |
|
| 43 | + @Test |
| 44 | + void whenUsingCustomQueryForNestedProjection_thenViewWithRequiredPropertiesIsReturned() { |
| 45 | + AddressView addressView = addressRepository.getViewAddressByState("CA").get(0); |
| 46 | + assertThat(addressView.getZipCode()).isEqualTo("90001"); |
| 47 | + |
| 48 | + PersonView personView = addressView.getPerson(); |
| 49 | + assertThat(personView.getFirstName()).isEqualTo("John"); |
| 50 | + assertThat(personView.getLastName()).isEqualTo("Doe"); |
| 51 | + } |
| 52 | + |
43 | 53 | @Test
|
44 | 54 | void whenUsingOpenProjections_thenViewWithRequiredPropertiesIsReturned() {
|
45 | 55 | PersonView personView = personRepository.findByLastName("Doe");
|
@@ -70,4 +80,15 @@ void whenUsingDynamicProjections_thenObjectWithRequiredPropertiesIsReturned() {
|
70 | 80 | assertThat(personView.getFirstName()).isEqualTo("John");
|
71 | 81 | assertThat(personDto.firstName()).isEqualTo("John");
|
72 | 82 | }
|
| 83 | + |
| 84 | + @Test |
| 85 | + void whenUsingDTOProjection_thenCorrectResultIsReturned() { |
| 86 | + List<AddressDto> addresses = addressRepository.findAddressByState("CA"); |
| 87 | + AddressDto address = addresses.get(0); |
| 88 | + assertThat(address.getZipCode()).isEqualTo("90001"); |
| 89 | + |
| 90 | + PersonDto person = address.getPerson(); |
| 91 | + assertThat(person.firstName()).isEqualTo("John"); |
| 92 | + assertThat(person.lastName()).isEqualTo("Doe"); |
| 93 | + } |
73 | 94 | }
|
0 commit comments