BAEL-3242: Use random port for test execution in spring-vertx module
This commit is contained in:
parent
9cd4a0a16e
commit
70ce72124b
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,27 +1,33 @@
|
|||
package com.baeldung.vertxspring;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
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.http.ResponseEntity;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
public class VertxSpringApplicationIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private Integer port;
|
||||
|
||||
private TestRestTemplate restTemplate = new TestRestTemplate();
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenReceivedArticles_thenSuccess() throws InterruptedException {
|
||||
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:8080/api/baeldung/articles", String.class);
|
||||
ResponseEntity<String> responseEntity = restTemplate
|
||||
.getForEntity("http://localhost:" + port + "/api/baeldung/articles", String.class);
|
||||
|
||||
assertEquals(200, responseEntity.getStatusCodeValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue