/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
- /*
- * Copyright 2012-2014 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.example.actuatorservice;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.boot.test.web.client.TestRestTemplate;
- import org.springframework.boot.web.server.LocalServerPort;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.test.context.TestPropertySource;
- import java.util.Map;
- import static org.assertj.core.api.BDDAssertions.then;
- @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
- @TestPropertySource(properties = {"local.management.port=7777"})
- public class HelloWorldApplicationTests {
- @LocalServerPort
- private int port;
- @Value("${local.management.port}")
- private int mgt;
- @Autowired
- private TestRestTemplate testRestTemplate;
- @Test
- public void shouldReturn200WhenSendingRequestToController() throws Exception {
- @SuppressWarnings("rawtypes")
- ResponseEntity<Map> entity = this.testRestTemplate.getForEntity(
- "http://localhost:" + this.port + "/hello-world", Map.class);
- then(entity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
- }
- @Test
- public void shouldReturn200WhenSendingRequestToManagementEndpoint() throws Exception {
- @SuppressWarnings("rawtypes")
- ResponseEntity<Map> entity = this.testRestTemplate.getForEntity(
- "http://localhost:" + this.mgt + "/actuator/info", Map.class);
- then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
- }
- }