Issue
I'm using RestAssured in my tests, in which I also mock the service layers. In order to do so, I had to change my test setup from webAppContextSetup
to standAloneSetup
. This way, I'm able to create an instance of my controller and @InjectMock
the mocked services into it.
Code:
@BeforeEach
public void setup() {
RestAssuredMockMvc.standaloneSetup(controller); -> Brakes test
RestAssuredMockMvc.webAppContextSetup(webApplicationContext); -> Works fine
}
The test is the following:
Mockito.when(findService.findUnreadNotificationItems(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(new PageImpl<>(List.of(generateDummyNotificationItemWithId())) {
});
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();
And the endpoint:
public Page<NotificationItemResponse> findUnreadNotifications(@PageableDefault(size = DEFAULT_PAGE_SIZE) final Pageable pageable,
@PathVariable final String groupName, @PathVariable final String userName)
When I use the standAloneSetup
I get the error:
No primary or single unique constructor found for interface org.springframework.data.domain.Pageable
I've seen a very similar question with good answers here, but RestAssuredMvc does not have any resolvers, or at least I found nothing on it searching the web. Can anyone help me resolve Pageables using RestAssuredMvc's standAloneSetup?
Solution
I found out that standaloneSetup has an overload that takes an MockMvcBuilder as argument. I could accomplish the customArgumentResolver with the following:
@BeforeEach
public void setup() {
RestAssuredMockMvc.standaloneSetup(MockMvcBuilders.standaloneSetup(controller).setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.setControllerAdvice(new NotificationItemControllerAdvice()));
}
Answered By - Pelicer
Answer Checked By - David Marino (JavaFixing Volunteer)