I've been struggling for a while with a test in which I purposely return an empty pageable result. This is my PageImpl implementation:
public class RestPageImpl<T> extends PageImpl<T> {
private static final long serialVersionUID = -3940752275692196046L;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public RestPageImpl(@JsonProperty("content") final List<T> content,
@JsonProperty("number") final int number,
@JsonProperty("size") final int size,
@JsonProperty("pageable") final JsonNode pageable,
@JsonProperty("last") final boolean last,
@JsonProperty("totalPages") final int totalPages,
@JsonProperty("sort") final JsonNode sort,
@JsonProperty("first") final boolean first,
@JsonProperty("numberOfElements") final int numberOfElements,
@JsonProperty("totalElements") final int totalElements) {
super(content, PageRequest.of(number, size), totalElements);
}
}
And in my test class, I'm using RestAssured to test my controller. This is the calling:
@Test
@SneakyThrows
void Should_Find_Unread_Notifications_Successfully() {
final NotificationItem notificationItem = generateDummyNotificationItemWithoutId();
notificationItemRepository.save(notificationItemMapper.toDocument(notificationItem));
final String response = given()
.queryParam("page", "0")
.queryParam("size", "10")
.contentType(ContentType.JSON)
.log().all()
.when()
.get("/notification/{groupName}/{userName}", "subacquiring_viewers", "John.Doe")
.then()
.log().all()
.assertThat()
.statusCode(HttpStatus.OK.value())
.extract().asString();
final Page<NotificationItemResponse> notificationItemResponsePage = mapper.readValue(response,
new TypeReference<RestPageImpl<NotificationItemResponse>>() {
});
assertEquals(1, notificationItemResponsePage.getNumberOfElements());
assertEquals(1, notificationItemResponsePage.getSize());
assertEquals(Sort.unsorted(), notificationItemResponsePage.getSort());
}
The test throws the following error: Cannot construct instance of 'com.project.notification.RestPageImpl', problem: Page size must not be less than one!. I have other tests with the exact same implementation working fine. The issue here seems to be the content being empty. How can I get the return of an empty page? I've been through this SO question and a lot of documentation, and couldn't figure it out.