22

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?

Silly Sally
  • 1,027
  • 2
  • 10
  • 15
  • 1
    They're different libraries, doing mostly the same thing, but possibly in different ways. There's a lot less differences than you're assuming. – Kayaman Aug 28 '18 at 06:53

3 Answers3

12

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.

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
11

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.

Yug Singh
  • 3,112
  • 5
  • 27
  • 52
6

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

Jet_C
  • 644
  • 9
  • 12