Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between MockMvc, RestAssured, and TestRestTemplate?

For all I know, MockMvc is just testing the Controller, and mocking the Service layer.

Whilst RestAssured and TestRestTemplate are testing the running instance of our API.

Is that correct? And what's the difference between RestAssured and Spring Boot's TestRestTemplate?

like image 913
Silly Sally Avatar asked Aug 28 '18 06:08

Silly Sally


People also ask

What is TestRestTemplate?

TestRestTemplate is not an extension of RestTemplate, but rather an alternative that simplifies integration testing and facilitates authentication during tests. It helps in customization of Apache HTTP client, but also it can be used as a wrapper of RestTemplate.

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.


3 Answers

As mentioned MockMvc is used to mock the service layer. It is useful in unit-testing of the code.

Whereas both RestAssured and TestRestTemplate are used for integration-testing which is end to end APIs testing.

Also, there is not much difference between RestAssured and Spring Boot's TestRestTemplate. You can use RestAssured for Spring-Boot Application or can go ahead with TestRestTemplate which is a Spring library.

like image 166
Yug Singh Avatar answered Nov 08 '22 20:11

Yug Singh


MockMvc is primarily used for web layer testing. Web layer testing is essentially writing fine-grained tests specifically designed to test your app’s controllers. It is very similar to writing regular unit tests for classes where you need mock dependencies for testing specific methods.

As far as comparing RestAssured vs TestRestTemplate they do pretty much the same thing. When it comes to RESTful based API integration testing and validation, TestRestTemplate and RestAssured both offer convenient methods to create and execute your HTTP calls with custom headers, auth, content types, query parameters, payload content, cookies, etc. The main difference -aside from syntax- is that TestRestTemplate is part of Spring’s test framework which comes bundled with the spring-boot-starter-test dependency.

Check out this article - Testing Spring Boot RESTful APIs using MockMvc/Mockito, Test RestTemplate and RestAssured - it has additional explanation and robust examples on the usage for all three (MockMvc, TestRestTemplate, and RestAssured).

like image 38
Jet_C Avatar answered Nov 08 '22 21:11

Jet_C


MockMvc is one of the classes in spring-test. This is primarily used for unit testing of the controller layer. Not just your controller class. This is for tetsing the controller layer. But you have to mock service and other layers. Hence it is primarily used for unit testing.

TestRestTemplate is again part of spring test, as the documentation says,

Convenient alternative of {@link RestTemplate} that is suitable for integration tests.

This can be used to test your Rest Service/ endpoints. One of the main difference is you use MockMvc for unit testing and TestRestTemplate for Integration testing. In other words, for using MockMvc, you don't need a running instance of server, but for TestRestTemplate you would need.

RestAssured is a completely different framework. This has nothing to do with Spring. This is a librariy, which provides various ways to test any REST service with fluent BDD style interface.

like image 8
pvpkiran Avatar answered Nov 08 '22 20:11

pvpkiran