gpt4 book ai didi

spring-mvc - 有没有办法将 AssertJ 断言与 Spring MVC 测试一起使用?

转载 作者:行者123 更新时间:2023-12-02 23:29:23 27 4
gpt4 key购买 nike

我在项目中使用 AssertJ 一段时间了。最近我开始使用 Spring MVC Test 来测试 Spring MVC Controller 。

但我不知道如何使用 AssertJ 。我在网上看到的所有示例都使用 Hamcrest 和 Spring MVC Test。

下面是使用 Hamcrest API 的示例。

mockMvc
.perform(get("/user?operation=userList"))
.andExpect(status().isOk())
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, userList))
.andExpect(view().name(UserController.VIEW_USER_LIST))
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasSize(2)))
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasItem(
allOf(
hasProperty("id", is(1L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Foo"))
)
)))
.andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasItem(
allOf(
hasProperty("id", is(2L)),
hasProperty("description", is("Lorem ipsum")),
hasProperty("title", is("Bar"))
)
)));

最佳答案

更新

如果您想投票支持使用 MockMvc 来支持 AssertJ 断言,请参阅相关的 Spring JIRA 问题:SPR-16637 .

<小时/>

一般来说,在使用 Spring 进行测试时,您可以选择任何您喜欢的断言框架。

但是,您所描述的特定场景涉及 Spring MVC 测试框架的 API。这些方法旨在与 Hamcrest Matcher API 一起使用。因此不可能在这些方法调用中使用 AssertJ。

问候,

Sam (Spring TestContext 框架的作者)

关于spring-mvc - 有没有办法将 AssertJ 断言与 Spring MVC 测试一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34447446/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com