Spring mvc 有没有办法在SpringMVC测试中使用AssertJ断言?

Spring mvc 有没有办法在SpringMVC测试中使用AssertJ断言?,spring-mvc,spring-test,hamcrest,assertj,Spring Mvc,Spring Test,Hamcrest,Assertj,我在项目中使用AssertJ已有一段时间了。最近,我开始使用SpringMVC测试来测试SpringMVC控制器 但是我不知道如何使用AssertJ。我在网上看到的所有示例都使用Hamcrest和Spring MVC测试 下面是使用Hamcrest API的示例 mockMvc .perform(get(“/user?operation=userList”)) .andExpect(状态().isOk()) .andExpect(model().attribute(UserController.

我在项目中使用AssertJ已有一段时间了。最近,我开始使用SpringMVC测试来测试SpringMVC控制器

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

下面是使用Hamcrest API的示例

mockMvc
.perform(get(“/user?operation=userList”))
.andExpect(状态().isOk())
.andExpect(model().attribute(UserController.model\u attribute\u USER\u LIST,userList))
.andExpect(view().name(UserController.view\u USER\u LIST))
.andExpect(model().attribute(UserController.model\u attribute\u USER\u LIST,hasSize(2)))
.andExpect(model().attribute(UserController.model_attribute_用户_列表,hasItem(
全部(
hasProperty(“id”,是(1L)),
hasProperty(“描述”,即“Lorem ipsum”),
hasProperty(“所有权”,即“Foo”))
)
)))
.andExpect(model().attribute(UserController.model_attribute_用户_列表,hasItem(
全部(
hasProperty(“id”,是(2L)),
hasProperty(“描述”,即“Lorem ipsum”),
hasProperty(“所有权”,即“律师”)
)
)));

更新

如果您想投票支持使用
MockMvc
包含对AssertJ断言的支持,请参阅相关的Spring JIRA问题:


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

但是,您描述的特定场景涉及SpringMVC测试框架的API。所讨论的方法设计用于Hamcrest
Matcher
API。因此,不可能在这些方法调用中使用AssertJ

问候,


Sam(SpringTestContext框架的作者)

最近,在讨论使用MockMvc添加对AssertJ断言的支持的项目中出现了一个问题,值得关注。您可以在此处查看此问题:


看起来需要包装MockMvc以提供对AssertJ断言的支持。

我已经建立了一个库,它为
MockMvc
以及
ResponseEntity
提供AssertJ断言(由
TestRestTemplate
返回):