RestAssuredMvc​​ 无法解析 Pageable

发布于 2025-01-16 14:28:17 字数 1835 浏览 1 评论 0原文

我在测试中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉 2025-01-23 14:28:17

我发现standaloneSetup有一个重载,它接受MockMvcBuilder作为参数。我可以通过以下方式完成 customArgumentResolver:

@BeforeEach
    public void setup() {
        RestAssuredMockMvc.standaloneSetup(MockMvcBuilders.standaloneSetup(controller).setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
                .setControllerAdvice(new NotificationItemControllerAdvice()));
    }

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()));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文