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"))
)
)));
Rich and easy to use AssertJ provides a rich set of assertions, truly helpful error messages, improves test code readability and is designed to be super easy to use within your favorite IDE.
AssertJ plugin installationlocation to : http://joel-costigliola.github.com/assertj-eclipse-plugin/repository/ If everything is ok, you should be able to select and install AssertJ plugin. The install takes some times (don't know why ...). Accept the licence agreement without reading it :) ...
Test: Todo Entries Are Found We can write an unit test for this controller method by following steps: Create the test data which is returned when our service method is called. We use a concept called test data builder when we are creating the test data for our test.
MockMvc provides support for Spring MVC testing. It encapsulates all web application beans and makes them available for testing. We'll initialize the mockMvc object in the @BeforeEach annotated method, so that we don't have to initialize it inside every test.
Introduction to AssertJ The AssertJ project provides fluent assertion statements for test code written in Java. These assert statements are typically used with Java JUnit tests. The base method for AssertJ assertions is the assertThat method followed by the assertion.
This tutorial describes the usage of the AssertJ framework for writing unit tests in Java. 1. Introduction to AssertJ The AssertJ project provides fluent assertion statements for test code written in Java. These assert statements are typically used with Java JUnit tests.
Maven Dependencies In order to use AssertJ, you need to include the following section in your pom.xml file: This dependency covers only the basic Java assertions. If you want to use advanced assertions, you will need to add additional modules separately. Note that for Java 7 and earlier you should use AssertJ core version 2.x.x.
The AssertJ project provides fluent assertion statements for Java. These assert statements are typically used with Java JUnit tests. AssertJ is a fork of the Fest assert library, as Fest is not actively maintained anymore. AssertJ is a library for simplifying the writing of assert statements in tests.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With