Is there a way to use AssertJ assertions with Spring MVC Test?
Asked Answered
E

3

23

I have been using AssertJ for some time in my projects. Recently I started using Spring MVC Test for testing Spring MVC controllers.

But I am not getting how to use AssertJ with it. All examples I see online all use Hamcrest with Spring MVC Test.

Below is an example using the 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"))
                        )
                )));
Ellingson answered 24/12, 2015 at 4:55 Comment(0)
S
18

Update - March 2024

Dedicated support for using AssertJ assertions with MockMvc is coming in Spring Framework 6.2.

See Spring Framework issue 21178 for details.


Update

If you would like to vote for inclusion of support for AssertJ assertions with MockMvc, please see the related Spring JIRA issue: SPR-16637.


Generally speaking, you may choose whatever assertion framework you like when testing with Spring.

However, the particular scenario you are describing involves the API of the Spring MVC Test framework. The methods in question are designed to be used with the Hamcrest Matcher API. It is therefore not possible to use AssertJ within those method calls.

Regards,

Sam (author of the Spring TestContext Framework)

Squatness answered 25/12, 2015 at 12:39 Comment(0)
D
3

I've put together a library that offers AssertJ assertions for MockMvc but also for ResponseEntity (returned by TestRestTemplate): https://github.com/ngeor/yak4j-spring-test-utils

Descend answered 28/1, 2019 at 20:39 Comment(0)
F
1

There has recently been an issue raised on the Spring Boot project to discuss adding support for AssertJ assertions with MockMvc, it might be worth keeping an eye on it. You can view the issue here: https://github.com/spring-projects/spring-boot/issues/5729

It looks like the initial concept created by Phil Webb involves wrapping the MockMvc to provide support for AssertJ assertions.

Frisk answered 20/4, 2016 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.