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;
|
package com.baeldung.autoconfig.exclude;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import com.baeldung.boot.Application;
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.RestAssured;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
|
import org.springframework.boot.web.server.LocalServerPort;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.baeldung.boot.Application;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@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")
|
@TestPropertySource(properties = "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration")
|
||||||
public class ExcludeAutoConfig1IntegrationTest {
|
public class ExcludeAutoConfig1IntegrationTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encapsulates the random port the test server is listening on.
|
||||||
|
*/
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
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);
|
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,32 @@
|
|||||||
package com.baeldung.autoconfig.exclude;
|
package com.baeldung.autoconfig.exclude;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import com.baeldung.boot.Application;
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.RestAssured;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
|
import org.springframework.boot.web.server.LocalServerPort;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.baeldung.boot.Application;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
@ActiveProfiles("test")
|
@ActiveProfiles("test")
|
||||||
public class ExcludeAutoConfig2IntegrationTest {
|
public class ExcludeAutoConfig2IntegrationTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encapsulates the random port the test server is listening on.
|
||||||
|
*/
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
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);
|
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,33 @@
|
|||||||
package com.baeldung.autoconfig.exclude;
|
package com.baeldung.autoconfig.exclude;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import com.baeldung.boot.Application;
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.RestAssured;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
|
import org.springframework.boot.web.server.LocalServerPort;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.baeldung.boot.Application;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
@EnableAutoConfiguration(exclude=SecurityAutoConfiguration.class)
|
@EnableAutoConfiguration(exclude=SecurityAutoConfiguration.class)
|
||||||
public class ExcludeAutoConfig3IntegrationTest {
|
public class ExcludeAutoConfig3IntegrationTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encapsulates the random port the test server is listening on.
|
||||||
|
*/
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
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);
|
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,29 @@
|
|||||||
package com.baeldung.autoconfig.exclude;
|
package com.baeldung.autoconfig.exclude;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.RestAssured;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
|
import org.springframework.boot.web.server.LocalServerPort;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = TestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
@SpringBootTest(classes = TestApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
public class ExcludeAutoConfig4IntegrationTest {
|
public class ExcludeAutoConfig4IntegrationTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encapsulates the random port the test server is listening on.
|
||||||
|
*/
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
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);
|
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,36 @@
|
|||||||
package com.baeldung.boot.autoconfig;
|
package com.baeldung.boot.autoconfig;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import com.baeldung.boot.Application;
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.RestAssured;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
|
import org.springframework.boot.web.server.LocalServerPort;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.baeldung.boot.Application;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
public class AutoConfigIntegrationTest {
|
public class AutoConfigIntegrationTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encapsulates the random port the test server is listening on.
|
||||||
|
*/
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNoAuthentication_whenAccessHome_thenUnauthorized() {
|
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);
|
assertEquals(HttpStatus.UNAUTHORIZED.value(), statusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAuthentication_whenAccessHome_thenOK() {
|
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);
|
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user