Skip to content

Commit ce9c615

Browse files
Merge pull request #18310 from sIvanovKonstantyn/master
BAEL-8891 - Use random port in integration test
2 parents e5e5e29 + 88dd239 commit ce9c615

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

libraries-5/src/test/java/com/baeldung/activej/ActiveJIntegrationTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import org.junit.jupiter.api.BeforeAll;
1313
import org.junit.jupiter.api.Test;
1414

15+
import java.io.IOException;
1516
import java.net.InetAddress;
17+
import java.net.InetSocketAddress;
18+
import java.net.ServerSocket;
1619

1720
import static org.junit.jupiter.api.Assertions.assertEquals;
1821

@@ -36,8 +39,8 @@ static void setUp() throws Exception {
3639
.build();
3740

3841
server = HttpServer.builder(eventloop, servlet)
39-
.withListenPort(8080)
40-
.build();
42+
.withListenPort(randomPort())
43+
.build();
4144

4245
server.listen();
4346

@@ -48,6 +51,14 @@ static void setUp() throws Exception {
4851
client = HttpClient.builder(eventloop, dnsClient).build();
4952
}
5053

54+
private static int randomPort() {
55+
try (ServerSocket socket = new ServerSocket(0)) {
56+
return socket.getLocalPort();
57+
} catch (IOException e) {
58+
throw new RuntimeException("Failed to find a free port", e);
59+
}
60+
}
61+
5162
@AfterAll
5263
static void tearDown() {
5364
if (server != null) {

0 commit comments

Comments
 (0)