Search code examples
springjava-17

Spring 6.1 Rendering Page object does not work as expected


I tried to upgrade my example project to Spring 6.1.0-M4, but some tests failed to due to the rending Page object.

It worked well with the old Spring 6.0.10, I am not sure if I need to extra config.

@RequiredArgsConstructor
@RestController
@RequestMapping("/posts")
@Validated
public class PostController {
    private final PostRepository posts;

    @GetMapping(value = "", produces = APPLICATION_JSON_VALUE)
    public ResponseEntity<Page<PostSummary>> getAll(@RequestParam(defaultValue = "") String q,
                                                    @RequestParam(defaultValue = "") String status,
                                                    @RequestParam(defaultValue = "0") int page,
                                                    @RequestParam(defaultValue = "10") int size) {
        var postStatus = StringUtils.hasText(status) ? Status.valueOf(status) : null;
        var data = this.posts.findAll(Specifications.findByKeyword(q, postStatus), PageRequest.of(page, size))
                .map(p -> new PostSummary(p.getTitle(), p.getCreatedAt()));
        return ok(data);
    }

And the testing codes look like.

@SpringJUnitWebConfig(classes = {Jackson2ObjectMapperConfig.class, WebConfig.class, TestDataConfig.class})
@ActiveProfiles("test")
public class PostControllerTestWithMockMvcWebTestClient {

    @Autowired
    PostController ctrl;

    WebTestClient rest;

    @Autowired
    PostRepository posts;

    @BeforeEach
    public void setup() {
        this.rest = MockMvcWebTestClient
                .bindToController(ctrl)
                .dispatcherServletCustomizer(dispatcherServlet -> dispatcherServlet.setEnableLoggingRequestDetails(true))
                .configureClient()
                .build();
    }

    @Test
    public void getAllPostsWillBeOk() throws Exception {
        when(this.posts.findAll(isA(Specification.class), isA(Pageable.class)))
                .thenReturn(new PageImpl<>(
                                List.of(
                                        Post.builder().title("test").content("content of test1").build(),
                                        Post.builder().title("test2").content("content of test2").build()
                                )
                        )
                );

        this.rest
                .get()
                .uri("/posts")
                .exchange()
                .expectStatus().isOk();
        // .expectBody().jsonPath("$.totalElements").isEqualTo(2);

        verify(this.posts, times(1)).findAll(isA(Specification.class), isA(Pageable.class));
        verifyNoMoreInteractions(this.posts);
    }


}

And finally I got the exception like this.

2023-08-22 13:25:10,579 DEBUG [main] org.springframework.core.log.LogFormatUtils: Writing [Page 1 of 1 containing com.example.demo.web.PostSummary instances]
2023-08-22 13:25:10,638 WARN  [main] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver: Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.UnsupportedOperationException)]
2023-08-22 13:25:10,666 DEBUG [main] org.springframework.core.log.LogFormatUtils: [4b544732] HTTP GET /posts
2023-08-22 13:25:10,676 DEBUG [main] org.springframework.core.log.LogFormatUtils: [4b544732] [21abda60] Response 500 INTERNAL_SERVER_ERROR
2023-08-22 13:25:10,745 ERROR [main] org.springframework.test.web.reactive.server.ExchangeResult: Request details for assertion failure:

> GET /posts
> WebTestClient-Request-Id: [1]

No content

< 500 INTERNAL_SERVER_ERROR Internal Server Error
< Content-Type: [application/json]

{"content":[{"title":"test","createdAt":null},{"title":"test2","createdAt":null}],"pageable":{"sort":{"empty":true,"sorted":false,"unsorted":true}}}

======================  MockMvc (Server) ===============================

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /posts
       Parameters = {}
          Headers = [WebTestClient-Request-Id:"1"]
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = com.example.demo.web.PostController
           Method = com.example.demo.web.PostController#getAll(String, String, int, int)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = org.springframework.http.converter.HttpMessageNotWritableException

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 500
    Error message = null
          Headers = [Content-Type:"application/json"]
     Content type = application/json
             Body = {"content":[{"title":"test","createdAt":null},{"title":"test2","createdAt":null}],"pageable":{"sort":{"empty":true,"sorted":false,"unsorted":true}}}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []



java.lang.AssertionError: Status expected:<200 OK> but was:<500 INTERNAL_SERVER_ERROR>
Expected :200 OK
Actual   :500 INTERNAL_SERVER_ERROR
<Click to see difference>


    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
    at org.springframework.test.web.reactive.server.StatusAssertions.lambda$assertStatusAndReturn$4(StatusAssertions.java:236)
    at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:222)
    at org.springframework.test.web.reactive.server.StatusAssertions.assertStatusAndReturn(StatusAssertions.java:236)
    at org.springframework.test.web.reactive.server.StatusAssertions.isOk(StatusAssertions.java:68)
    at com.example.demo.web.PostControllerTestWithMockMvcWebTestClient.getAllPostsWillBeOk(PostControllerTestWithMockMvcWebTestClient.java:63)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)


The example project codes is here, https://github.com/hantsy/spring6-sandbox/tree/master/data-jpa


Solution

  • In the new Spring Data, the new PageImpl(data) will use a new UnPaged class to assemble the page data, and the exception was raised by the UnPaged.getOffset() method.

    Simply we can use a custom class to represent the paged result, for example:

    public record PaginatedResult<T>(List<T> data, Long count){}
    

    And in controller try to convert the returned Page<T> to the PaginatedResult<T>.

    new PaginatedResult(page.getContent(), page.getTotalElementCount());
    

    In the testing codes, the controller will return response body like:

    new PaginatedResult(
        listOf(...), 
        count
    );