diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/paging/CustomerService.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/paging/CustomerService.java index cb426084ca..73affeb1a1 100644 --- a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/paging/CustomerService.java +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/paging/CustomerService.java @@ -15,7 +15,7 @@ public class CustomerService { this.customerRepository = customerRepository; } - private final CustomerRepository customerRepository; + private final CustomerRepository customerRepository; public Page getCustomers(int page, int size) { @@ -25,13 +25,11 @@ public class CustomerService { int start = (int) pageRequest.getOffset(); int end = Math.min((start + pageRequest.getPageSize()), allCustomers.size()); - - return new PageImpl<>(allCustomers.subList(start, end), pageRequest, allCustomers.size()); - + List pageContent = allCustomers.subList(start, end); + return new PageImpl<>(pageContent, pageRequest, allCustomers.size()); } private Pageable createPageRequestUsing(int page, int size) { return PageRequest.of(page, size); - } } diff --git a/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/paging/CustomerControllerTest.java b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/paging/CustomerControllerTest.java index 112ab545aa..d199c56435 100644 --- a/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/paging/CustomerControllerTest.java +++ b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/paging/CustomerControllerTest.java @@ -9,7 +9,6 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; -import org.assertj.core.api.Assertions; import org.json.JSONObject; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -23,7 +22,6 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; - @WebMvcTest(CustomerRestController.class) public class CustomerControllerTest { @@ -62,15 +60,10 @@ public class CustomerControllerTest { MockHttpServletResponse response = result.getResponse(); JSONObject jsonObject = new JSONObject(response.getContentAsString()); - assertThat(jsonObject.get("totalPages")) - .isEqualTo(4); - assertThat(jsonObject.get("totalElements")) - .isEqualTo(20); - assertThat(jsonObject.get("number")) - .isEqualTo(1); - assertThat(jsonObject.get("size")) - .isEqualTo(5); - assertThat(jsonObject.get("content")) - .isNotNull(); + assertThat(jsonObject.get("totalPages")).isEqualTo(4); + assertThat(jsonObject.get("totalElements")).isEqualTo(20); + assertThat(jsonObject.get("number")).isEqualTo(1); + assertThat(jsonObject.get("size")).isEqualTo(5); + assertThat(jsonObject.get("content")).isNotNull(); } } diff --git a/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/paging/CustomerServiceTest.java b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/paging/CustomerServiceTest.java index 0d5e4879db..4664cd2424 100644 --- a/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/paging/CustomerServiceTest.java +++ b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/paging/CustomerServiceTest.java @@ -8,7 +8,6 @@ import java.util.Collection; import java.util.List; import java.util.stream.Collectors; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; @@ -27,16 +26,9 @@ public class CustomerServiceTest { @InjectMocks private CustomerService customerService; - private static final List ALL_CUSTOMERS = Arrays.asList( - new Customer("Ali"), new Customer("Brian"), new Customer("Coddy"), - new Customer("Di"), new Customer("Eve"), new Customer("Fin"), - new Customer("Grace"), new Customer("Harry"), - new Customer("Ivan"), new Customer("Judy"), - new Customer("Kasim"), new Customer("Liam"), - new Customer("Mike"),new Customer("Nick"), - new Customer("Omar"), new Customer("Penny"), - new Customer("Queen"),new Customer("Rob"), - new Customer("Sue"),new Customer("Tammy")); + private static final List ALL_CUSTOMERS = Arrays.asList(new Customer("Ali"), new Customer("Brian"), new Customer("Coddy"), new Customer("Di"), new Customer("Eve"), new Customer("Fin"), new Customer("Grace"), new Customer("Harry"), + new Customer("Ivan"), new Customer("Judy"), new Customer("Kasim"), new Customer("Liam"), new Customer("Mike"), new Customer("Nick"), new Customer("Omar"), new Customer("Penny"), new Customer("Queen"), new Customer("Rob"), new Customer("Sue"), + new Customer("Tammy")); private static final List PAGE_1_CONTENTS = Arrays.asList("Ali", "Brian", "Coddy", "Di", "Eve"); @@ -47,20 +39,14 @@ public class CustomerServiceTest { private static final List PAGE_4_CONTENTS = Arrays.asList("Penny", "Queen", "Rob", "Sue", "Tammy"); private static final List EMPTY_PAGE = Arrays.asList(); + @BeforeEach void setup() { when(customerRepository.findAll()).thenReturn(ALL_CUSTOMERS); } private static Collection testIO() { - return Arrays.asList( - new Object[][] { - { 0, 5, PAGE_1_CONTENTS, 20L, 4L }, - { 1, 5, PAGE_2_CONTENTS, 20L, 4L }, - { 2, 5, PAGE_3_CONTENTS, 20L, 4L }, - { 3, 5, PAGE_4_CONTENTS, 20L, 4L }, - { 4, 5, EMPTY_PAGE, 20L, 4L } } - ); + return Arrays.asList(new Object[][] { { 0, 5, PAGE_1_CONTENTS, 20L, 4L }, { 1, 5, PAGE_2_CONTENTS, 20L, 4L }, { 2, 5, PAGE_3_CONTENTS, 20L, 4L }, { 3, 5, PAGE_4_CONTENTS, 20L, 4L }, { 4, 5, EMPTY_PAGE, 20L, 4L } }); } @ParameterizedTest