Fixed the Port Allocation Failure by Listening to Random Port
This commit is contained in:
parent
6bca22524a
commit
6e1846d8fa
|
@ -1,26 +1,32 @@
|
|||
package com.baeldung.autoconfig.exclude;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import com.baeldung.boot.Application;
|
||||
import io.restassured.RestAssured;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@TestPropertySource(properties = "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration")
|
||||
public class ExcludeAutoConfig1IntegrationTest {
|
||||
|
||||
/**
|
||||
* Encapsulates the random port the test server is listening on.
|
||||
*/
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
||||
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
|
||||
int statusCode = RestAssured.get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,32 @@
|
|||
package com.baeldung.autoconfig.exclude;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import com.baeldung.boot.Application;
|
||||
import io.restassured.RestAssured;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@ActiveProfiles("test")
|
||||
public class ExcludeAutoConfig2IntegrationTest {
|
||||
|
||||
/**
|
||||
* Encapsulates the random port the test server is listening on.
|
||||
*/
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
||||
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
|
||||
int statusCode = RestAssured.get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,27 +1,33 @@
|
|||
package com.baeldung.autoconfig.exclude;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import com.baeldung.boot.Application;
|
||||
import io.restassured.RestAssured;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@EnableAutoConfiguration(exclude=SecurityAutoConfiguration.class)
|
||||
public class ExcludeAutoConfig3IntegrationTest {
|
||||
|
||||
/**
|
||||
* Encapsulates the random port the test server is listening on.
|
||||
*/
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
||||
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
|
||||
int statusCode = RestAssured.get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,29 @@
|
|||
package com.baeldung.autoconfig.exclude;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import io.restassured.RestAssured;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = TestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||
@SpringBootTest(classes = TestApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class ExcludeAutoConfig4IntegrationTest {
|
||||
|
||||
/**
|
||||
* Encapsulates the random port the test server is listening on.
|
||||
*/
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
||||
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
|
||||
int statusCode = RestAssured.get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +1,36 @@
|
|||
package com.baeldung.boot.autoconfig;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import com.baeldung.boot.Application;
|
||||
import io.restassured.RestAssured;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class AutoConfigIntegrationTest {
|
||||
|
||||
/**
|
||||
* Encapsulates the random port the test server is listening on.
|
||||
*/
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void givenNoAuthentication_whenAccessHome_thenUnauthorized() {
|
||||
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
|
||||
int statusCode = RestAssured.get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.UNAUTHORIZED.value(), statusCode);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthentication_whenAccessHome_thenOK() {
|
||||
int statusCode = RestAssured.given().auth().basic("john", "123").get("http://localhost:8080/").statusCode();
|
||||
int statusCode = RestAssured.given().auth().basic("john", "123").get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue