Fix the integration tests which failed due to defined port (#14215)

This commit is contained in:
Saikat Chakraborty 2023-06-11 13:15:27 +05:30 committed by GitHub
parent 237893ed94
commit 3eda2da25f
2 changed files with 17 additions and 9 deletions

View File

@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
@ -14,20 +15,23 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(classes = SpringSecurityApplication.class, @SpringBootTest(classes = SpringSecurityApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
class ApiControllerIntegrationTest { class ApiControllerIntegrationTest {
private final TestRestTemplate restTemplate = new TestRestTemplate(); private final TestRestTemplate restTemplate = new TestRestTemplate();
private static final String API_ENDPOINT = "http://localhost:8080/app/api/hello"; private static final String API_ENDPOINT = "http://localhost:%s/app/api/hello";
@LocalServerPort
private int serverPort;
@Test @Test
void givenAuthHeaderSecretIsValid_whenApiControllerCalled_thenReturnOk() throws Exception { void givenAuthHeaderSecretIsValid_whenApiControllerCalled_thenReturnOk() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add("x-auth-secret-key", "test-secret"); headers.add("x-auth-secret-key", "test-secret");
ResponseEntity<String> response = restTemplate.exchange(new URI(API_ENDPOINT), HttpMethod.GET, ResponseEntity<String> response = restTemplate.exchange(new URI(String.format(API_ENDPOINT, serverPort)), HttpMethod.GET,
new HttpEntity<>(headers), String.class); new HttpEntity<>(headers), String.class);
assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals(HttpStatus.OK, response.getStatusCode());
@ -39,7 +43,7 @@ class ApiControllerIntegrationTest {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add("x-auth-secret-key", "invalid"); headers.add("x-auth-secret-key", "invalid");
ResponseEntity<String> response = restTemplate.exchange(new URI(API_ENDPOINT), HttpMethod.GET, ResponseEntity<String> response = restTemplate.exchange(new URI(String.format(API_ENDPOINT, serverPort)), HttpMethod.GET,
new HttpEntity<>(headers), String.class); new HttpEntity<>(headers), String.class);
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
@ -50,7 +54,7 @@ class ApiControllerIntegrationTest {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add("x-auth-secret", "test-secret"); headers.add("x-auth-secret", "test-secret");
ResponseEntity<String> response = restTemplate.exchange(new URI(API_ENDPOINT), HttpMethod.GET, ResponseEntity<String> response = restTemplate.exchange(new URI(String.format(API_ENDPOINT, serverPort)), HttpMethod.GET,
new HttpEntity<>(headers), String.class); new HttpEntity<>(headers), String.class);
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
@ -60,7 +64,7 @@ class ApiControllerIntegrationTest {
void givenAuthHeaderIsMissing_whenApiControllerCalled_thenReturnUnAuthorised() throws Exception { void givenAuthHeaderIsMissing_whenApiControllerCalled_thenReturnUnAuthorised() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
ResponseEntity<String> response = restTemplate.exchange(new URI(API_ENDPOINT), HttpMethod.GET, ResponseEntity<String> response = restTemplate.exchange(new URI(String.format(API_ENDPOINT, serverPort)), HttpMethod.GET,
new HttpEntity<>(headers), String.class); new HttpEntity<>(headers), String.class);
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());

View File

@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
@ -13,19 +14,22 @@ import java.net.URI;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(classes = SpringSecurityApplication.class, @SpringBootTest(classes = SpringSecurityApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
class HealthCheckControllerIntegrationTest { class HealthCheckControllerIntegrationTest {
private final TestRestTemplate restTemplate = new TestRestTemplate(); private final TestRestTemplate restTemplate = new TestRestTemplate();
private static final String HEALTH_CHECK_ENDPOINT = "http://localhost:8080/app/health"; private static final String HEALTH_CHECK_ENDPOINT = "http://localhost:%s/app/health";
@LocalServerPort
private int serverPort;
@Test @Test
void givenApplicationIsRunning_whenHealthCheckControllerCalled_thenReturnOk() throws Exception { void givenApplicationIsRunning_whenHealthCheckControllerCalled_thenReturnOk() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
ResponseEntity<String> response = restTemplate.exchange(new URI(HEALTH_CHECK_ENDPOINT), HttpMethod.GET, ResponseEntity<String> response = restTemplate.exchange(new URI(String.format(HEALTH_CHECK_ENDPOINT, serverPort)), HttpMethod.GET,
new HttpEntity<>(headers), String.class); new HttpEntity<>(headers), String.class);
assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals(HttpStatus.OK, response.getStatusCode());