1

I was trying to run Spring Boot Microservices Tests, which are explained in this article: https://blog.codecentric.de/en/2017/02/integration-testing-strategies-spring-boot-microservices-part-2/

In these tests, Spring Boot Application is started and stopped programmatically before and after each test, using Spring RestTemplate Client and Spring Boot Actuator "shutdown" endpoint.

Unfortunately, this code doesn't work in Spring Boot 2.3.1 and returns "Error 415 Unsupported Media Type"

ResponseEntity<JSONObject> response = template
                        .postForEntity(managementUrl + "/shutdown", "", JSONObject.class);

The application has to be killed manually in administration console after the tests.

Full source code can be found on GitLab: https://gitlab.com/dfeingol/springboot-testing-tips/-/tree/master/atdd

This is a really interesting testing strategy and a great alternative to using Spring Boot Docker Images in the tests.

Unfortunately, the article and the source code are very old and use Spring Boot 1.4.0

Does anyone know how to shutdown Spring Boot 2.3.1 application correctly, using Spring Boot Actuator "shutdown" endpoint and Spring RestTemplate Client?

Mykhailo Skliar
  • 1,242
  • 1
  • 8
  • 19

2 Answers2

2

You are missing the HttpHeader, please see the answer:

POST request via RestTemplate in JSON

Also you need to enable the endpoint and expose it over HTTP:

management.endpoints.web.expose=*
management.endpoint.shutdown.enabled=true
Umesh Sanwal
  • 681
  • 4
  • 13
1

Thank you for your help, Umesh Sanwal!

The following code worked for me:

  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_JSON);
  HttpEntity<String> entity = new HttpEntity<String>(null, headers);
  ResponseEntity<String> response = template.postForEntity(managementUrl + "/shutdown", entity, String.class);

I was able to update the code from the article to the latest Spring Boot (2.3.1) and Cucumber (6.2.2) and fixed all the tests:

See the article on Spring Boot Microservices Testing Strategies: https://blog.codecentric.de/en/2017/02/integration-testing-strategies-spring-boot-microservices-part-2/

See the full updated code on my GitHub: https://github.com/skyglass/skyglass-composer/tree/master/springboot-testing-tips

Mykhailo Skliar
  • 1,242
  • 1
  • 8
  • 19