RestAssuredMvc 无法解析 Pageable
我在测试中使用 RestAssured,其中我还模拟了服务层。为此,我必须将测试设置从 webAppContextSetup
更改为 standAloneSetup
。这样,我就可以创建控制器的实例并将模拟服务添加到其中。
代码:
@BeforeEach
public void setup() {
RestAssuredMockMvc.standaloneSetup(controller); -> Brakes test
RestAssuredMockMvc.webAppContextSetup(webApplicationContext); -> Works fine
}
测试如下:
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();
端点:
public Page<NotificationItemResponse> findUnreadNotifications(@PageableDefault(size = DEFAULT_PAGE_SIZE) final Pageable pageable,
@PathVariable final String groupName, @PathVariable final String userName)
当我使用standAloneSetup
时,我收到错误:
No primary or single unique constructor found for interface org.springframework.data.domain.Pageable
我看到了一个非常相似的问题,但答案很好此处,但 RestAssuredMvc 没有任何解析器,或者至少我什么也没找到在网上搜索它。 任何人都可以帮助我使用 RestAssuredMvc 的standAloneSetup 解决 Pageables 问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现standaloneSetup有一个重载,它接受MockMvcBuilder作为参数。我可以通过以下方式完成 customArgumentResolver:
I found out that standaloneSetup has an overload that takes an MockMvcBuilder as argument. I could accomplish the customArgumentResolver with the following: