Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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"))
                        )
                )));
like image 252
ashishjmeshram Avatar asked Dec 24 '15 04:12

ashishjmeshram


People also ask

Why use AssertJ?

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.

How to install AssertJ in eclipse?

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 :) ...

What technique is used to unit test on a controller class?

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.

What is MockMvc used for?

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.

What is assertion in assertj?

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.

What is assertj framework?

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.

How do I use assertj in Maven?

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.

What is assertj In JUnit?

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.


2 Answers

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)

like image 157
Sam Brannen Avatar answered Oct 04 '22 13:10

Sam Brannen


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

like image 28
Nikolaos Georgiou Avatar answered Oct 04 '22 12:10

Nikolaos Georgiou