Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestFuse vs Rest Assured vs MockMVC Rest Service Unit Test Framework

I've been trying to find a simple all purpose unit test framework for Spring MVC based Rest Services I've written.

I've been searching online and narrowed it down to:

  • RestFuse (http://developer.eclipsesource.com/restfuse/)
  • Rest Assured (https://github.com/jayway/rest-assured)
  • MockMVC (http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-rest-api/)

I like RestFuse because it's mostly annotation based, but rest assured seems to have a easier way of passing parameters and checking responses. And finally being a Spring MVC Rest Service project, I'm wondering if I should just stick with the already established way of testing Rest Services in Spring with MockMVC.

Interested to get any feedback, as well as performance, past experiences and if there's anything else I should take into consideration.

like image 280
JackDev Avatar asked Jan 07 '16 01:01

JackDev


People also ask

What is MockMvc test?

What is MockMvc? MockMvc is a Spring Boot test tool class that lets you test controllers without needing to start an HTTP server. In these tests the application context is loaded and you can test the web layer as if i's receiving the requests from the HTTP server without the hustle of actually starting it.

Should I use MockMvc?

As said in this article you should use MockMvc when you want to test Server-side of application: Spring MVC Test builds on the mock request and response from spring-test and does not require a running servlet container.

Is MockMvc integration test?

Technically speaking, tests using MockMvc are in the boundaries between unit and integration tests. They aren't unit tests because endpoints are tested in integration with a Mocked MVC container with mocked inputs and dependencies.

Why MockMvc is used?

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.


2 Answers

Rest-Assured is gaining acceptance over the other frameworks to test REST services in Java. Having BDD style fluent interface, its easy to read the test scripts also, with minimal learning curve.

I am using this framework for verifying REST services as an end user, and it has been easier to implement test scripts for them. Hence I cannot comment much on Spring MVC part of REST-assured.

However, this blog post gives you more details on RestAssured v2.2 which includes spring-mock-mvc module built on top of MockMVC giving BDD style fluent interface flavor through REST assured. The blog post also cautions:

When not to use it:

RestAssuredMockMvc is not to be considered a complete replacement to vanilla MockMvc since it contains more specific features coupled to Spring MVC. For example right now there’s no first class support for things like flash attributes and principals. You can how ever add those by using an interceptor. Standard REST Assured also supports a lot of different authentication schemes and filters which are not available in the RestAssuredMockMvc API. Another reason for why you may want to use the standard REST Assured API is if it’s important to your organization to test the REST API in a real container (for example if you’ve configured authentication or authorization in a container specific manner) or if you’re using JAX-RS (or any other framework regardless of language).

Finally, have a look at REST-Assured spring-mvc-webapp examples in REST-assured codebase, and decide, whether you like to give a try for it and make the best use of both REST-assured and MockMVC frameworks.

like image 61
parishodak Avatar answered Sep 22 '22 12:09

parishodak


The beauty of MockMVC is that it provides a mock servlet container, allowing you to integration-test your REST services without deploying to a web server. I believe that you can still leverage this power when using REST Assured with the spring-mock-mvc module.

Another framework that I just learned of is Karate. Its author is currently experimenting with a mechanism to allow execution in a mock servlet container (see Peter Thomas' answer to Is there a mechanism for integration testing JAX-RS services without deploying (a la MockMVC)?).

like image 45
Daniel Steinberg Avatar answered Sep 23 '22 12:09

Daniel Steinberg