1

I have build a REST API Based SMS gateway on Spring boot, along with an extensive testing suite comprising of unit and integration tests for testing the business logic and various layers of the API's architecture. I am now required to create a test which consumes the API call that runs from my localhost under various conditions. My queries are as follows:

  1. Do I need to separately run the Spring Boot application on localhost before I can run the tests that directly consume the API from localhost?

  2. I need to integrate my tests with Travis for Continuous integration. Is it possible to build an integration test which, on being run, starts the Spring application on localhost and directly calls the API URL from localhost for testing its response when different parameters are passed to the URL?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Usman Khaliq
  • 363
  • 3
  • 22
  • 1. use `jersey WebResource` from `sun` (com.sun.jersey.api.client.WebResource) and 2. mockmvc from (org.springframework.test.web.servlet.MockMvc) :D – Amir Azizkhani Jan 15 '18 at 11:50
  • I am currently looking into RestAssured for creating a test that consumes the API end-point. Would you recommend it, and also have you worked on integrating RestAssured with the build environment of a Spring boot project before? Also, with MockMvc, won't I need to mock the response from the end-point? I have already tested the various components of the API with my unit and integration tests ( controller, service layer, data access layer etc), and now need to consume the API end point without mocking anything. – Usman Khaliq Jan 15 '18 at 12:26
  • I am using `MockMvc` now and not see `RestAssured` yet; On the other hand `MockMvc` does not mock response data. see this full documentation : https://docs.spring.io/spring-security/site/docs/current/reference/html/test-mockmvc.html – Amir Azizkhani Jan 15 '18 at 12:33
  • noted. let me check out the documentation and revert back to you in case i have any queries or when I have been able to successfully build up the required tests. thanks! – Usman Khaliq Jan 15 '18 at 12:37
  • @Usman - RestAssured is better choice compared to MockMvc, since it doesn't mock your request and depicts real scenario. – Yogi Jan 15 '18 at 13:10
  • thanks @Yogi , I am still deciding between RestAssured and MockMvc . I came across this answer ( https://stackoverflow.com/a/34659934/4623619) where they recommended MockMvc, but only just. Have you integrated RestAssured with a spring boot application before? – Usman Khaliq Jan 15 '18 at 13:30
  • 1
    Yes, you can see this project. It has only Test for Controller... https://github.com/yogi21jan/spring-hateos-restassured-demo – Yogi Jan 15 '18 at 14:22
  • thanks @Yogi the repo link was helpful! just one clarification that I require. I need to create a test that fails when the API end point is accessed without the correct authentication. I am able to get the 401 error from Postman when i try to access the end point without the authentication. However, in RestAssured, I am able to access the end point properly even without authentication. Any idea on what I should be looking into to correct this? – Usman Khaliq Jan 15 '18 at 17:24
  • thanks @Yogi ! RestAssured worked well for my solution! – Usman Khaliq Jan 17 '18 at 10:37

1 Answers1

0

Following from Yogi's comment above, I used RestAssured to build a test suite that directly consumed the API end point running on my localhost.

The following bookmarks helped me understand RestAssured:

For getting a general understanding of how Rest Assured can be used: https://semaphoreci.com/community/tutorials/testing-rest-endpoints-using-rest-assured

The official RestAssured wiki for understanding the API: https://github.com/rest-assured/rest-assured/wiki/Usage#getting-response-data

An example of using RestAssured via Gradle in a Spring project: https://www.blazemeter.com/blog/how-to-extract-values-when-api-testing-with-rest-assured

Using Basic Authentication with Rest Assured: https://www.youtube.com/watch?v=PAyGma2OMFo

Usman Khaliq
  • 363
  • 3
  • 22