/boot/actuator-service/src/test/java/com/example/actuatorservice/HelloWorldApplicationTests.java

https://gitlab.com/avincze73/spring-20220325 · Java · 64 lines · 36 code · 13 blank · 15 comment · 0 complexity · 4558c0b79e921933bc3ebf3099ea394c MD5 · raw file

  1. /*
  2. * Copyright 2012-2014 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.actuatorservice;
  17. import org.junit.jupiter.api.Test;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.boot.test.context.SpringBootTest;
  21. import org.springframework.boot.test.web.client.TestRestTemplate;
  22. import org.springframework.boot.web.server.LocalServerPort;
  23. import org.springframework.http.HttpStatus;
  24. import org.springframework.http.ResponseEntity;
  25. import org.springframework.test.context.TestPropertySource;
  26. import java.util.Map;
  27. import static org.assertj.core.api.BDDAssertions.then;
  28. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  29. @TestPropertySource(properties = {"local.management.port=7777"})
  30. public class HelloWorldApplicationTests {
  31. @LocalServerPort
  32. private int port;
  33. @Value("${local.management.port}")
  34. private int mgt;
  35. @Autowired
  36. private TestRestTemplate testRestTemplate;
  37. @Test
  38. public void shouldReturn200WhenSendingRequestToController() throws Exception {
  39. @SuppressWarnings("rawtypes")
  40. ResponseEntity<Map> entity = this.testRestTemplate.getForEntity(
  41. "http://localhost:" + this.port + "/hello-world", Map.class);
  42. then(entity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
  43. }
  44. @Test
  45. public void shouldReturn200WhenSendingRequestToManagementEndpoint() throws Exception {
  46. @SuppressWarnings("rawtypes")
  47. ResponseEntity<Map> entity = this.testRestTemplate.getForEntity(
  48. "http://localhost:" + this.mgt + "/actuator/info", Map.class);
  49. then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
  50. }
  51. }