Use random port in spring-session-mongodb IntegrationTest
@@ -6,6 +6,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
@@ -16,9 +17,12 @@ import java.util.Base64;
@RunWith(SpringRunner.class)
-@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
+@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringSessionMongoDBIntegrationTest {
+ @LocalServerPort
+ private int port;
+
@Autowired
private MongoOperationsSessionRepository repository;
@@ -27,7 +31,7 @@ public class SpringSessionMongoDBIntegrationTest {
@Test
public void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() {
HttpEntity<String> response = restTemplate
- .exchange("http://localhost:" + 8080, HttpMethod.GET, null, String.class);
+ .exchange("http://localhost:" + port, HttpMethod.GET, null, String.class);
HttpHeaders headers = response.getHeaders();
String set_cookie = headers.getFirst(HttpHeaders.SET_COOKIE);
