10

Which of the 3 APIs: TestRestTemplate, WebTestClient, RestAssured is the most efficient for integration testing of Spring Boot Rest API (non-reactive) with respect to the following:

  1. Ensures compatibility with Spring boot for incremental versions.
  2. Easy-to-use and extensive assertions for checking status codes, headers, both JSON/XML payload validation, response time, etc.
  3. Has the possibility to test spring container configuration.
  4. Features to reuse specific checks across all end points.
  5. Is the most stable and therefore an Industry common practice.

Few initial findings which sort of prevent one of them to be marked as clear winner are:

  • TestRestTemplate & WebTestClient are Spring built-in APIs so are auto-configurable with Spring Boot. Latest versions of RestAssured (4.0.0 onwards) can have problems with spring boot(java.lang.NoClassDefFoundError: io/restassured/mapper/factory/GsonObjectMapperFactory) and require explicit dependencies addition.

  • WebTestClient (a Fluent API) seems to have nice and easy to use syntax as compared to TestRestTemplate but is very new and there are not enough examples showing its usage for non-reactive Rest APIs and availability of extensive assertion API

  • RestTemplate (API on which TestRestTemplate is based) will be deprecated in future Spring Boot version.

Feedbacks from experienced developers will be a great help.

Seema Garg
  • 153
  • 1
  • 9

2 Answers2

6

I chose WebTestClient and I am happy with it. As you wrote WebTestClient is built-in Spring Boot so this was main reason why I started with WebTestClient because I didn't want to add another dependecy if I have pretty good solution out of the box. It doesn't matter if your application is reactive or no you can use WebTestClient. It supports JSONPath. I have experience with TestRestTemplate too but I think that WebTestClient is much better.

Saljack
  • 2,072
  • 21
  • 24
6

When doing Integration testing in Spring Boot environment, currently both TestRestTemplate and WebTestClient can be used based upon need as of now.

Yes, WebTestClient is newly introduced with Spring 5 targeting the reactive (non-blocking) way of integration testing where, endpoint will not be connected until it is subscribed or consumed. Also, it has been designed with Fluent way so easy to use. Spring team also encouraging to use this only reason is they are planning not to give any new feature in TestRestTemplate (it is in maintenance mode currently) and this makes very easy to take decisions for use.

  1. If your project has already TestRestTemplate used, keep as it is, reason is there is no formal deprecation announced
  2. If you are working on new project or writing new tests, then need to use WebTestClient as per Spring team recommendation

Hope, it will guide you to take decision.

Sandeep Kumar
  • 596
  • 5
  • 7