BAEL-3242: Use random port for test execution in spring-vertx module
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.vertxspring.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
|
||||
@Configuration
|
||||
public class PortConfiguration {
|
||||
|
||||
private static final int DEFAULT_PORT = 8069;
|
||||
|
||||
|
||||
@Profile("default")
|
||||
@Bean
|
||||
public Integer defaultPort() {
|
||||
return DEFAULT_PORT;
|
||||
}
|
||||
|
||||
@Profile("test")
|
||||
@Bean
|
||||
public Integer randomPort() {
|
||||
try (ServerSocket socket = new ServerSocket(0)) {
|
||||
return socket.getLocalPort();
|
||||
|
||||
} catch (IOException e) {
|
||||
return DEFAULT_PORT;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.vertxspring.verticles;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
@@ -9,6 +10,9 @@ import io.vertx.ext.web.RoutingContext;
|
||||
@Component
|
||||
public class ServerVerticle extends AbstractVerticle {
|
||||
|
||||
@Autowired
|
||||
private Integer defaultPort;
|
||||
|
||||
private void getAllArticlesHandler(RoutingContext routingContext) {
|
||||
vertx.eventBus()
|
||||
.<String>send(ArticleRecipientVerticle.GET_ALL_ARTICLES, "", result -> {
|
||||
@@ -36,7 +40,7 @@ public class ServerVerticle extends AbstractVerticle {
|
||||
|
||||
vertx.createHttpServer()
|
||||
.requestHandler(router::accept)
|
||||
.listen(config().getInteger("http.port", 8080));
|
||||
.listen(config().getInteger("http.port", defaultPort));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user