Use random port in spring-boot tests

Update several tests to use random ports instead of hard coding '8080'
or '8081'.

fixes gh-607
pull/660/merge
Stephane Nicoll 11 years ago committed by Phillip Webb
parent fca7a8dc96
commit af33cc2b97

@ -541,7 +541,7 @@ public class SpringApplicationTests {
@Bean
public JettyEmbeddedServletContainerFactory container() {
return new JettyEmbeddedServletContainerFactory();
return new JettyEmbeddedServletContainerFactory(0);
}
}

@ -44,10 +44,12 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.SocketUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.concurrent.ListenableFuture;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@ -88,18 +90,17 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration());
this.container.start();
assertThat(getResponse("http://localhost:8080/hello"), equalTo("Hello World"));
assertThat(getResponse(getLocalUrl("/hello")), equalTo("Hello World"));
}
@Test
public void emptyServerWhenPortIsZero() throws Exception {
public void emptyServerWhenPortIsMinusOne() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
factory.setPort(0);
factory.setPort(-1);
this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration());
this.container.start();
this.thrown.expect(IOException.class);
getResponse("http://localhost:8080/hello");
assertThat(this.container.getPort(), lessThan(0)); // Jetty is -2
}
@Test
@ -110,7 +111,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container.start();
this.container.stop();
this.thrown.expect(IOException.class);
getResponse("http://localhost:8080/hello");
getResponse(getLocalUrl("/hello"));
}
@Test
@ -121,8 +122,8 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container.start();
HttpComponentsAsyncClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory();
ListenableFuture<ClientHttpResponse> response1 = clientHttpRequestFactory
.createAsyncRequest(new URI("http://localhost:8080/hello"),
HttpMethod.GET).executeAsync();
.createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET)
.executeAsync();
assertThat(response1.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200));
this.container.stop();
@ -131,8 +132,8 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container.start();
ListenableFuture<ClientHttpResponse> response2 = clientHttpRequestFactory
.createAsyncRequest(new URI("http://localhost:8080/hello"),
HttpMethod.GET).executeAsync();
.createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET)
.executeAsync();
assertThat(response2.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200));
}
@ -143,7 +144,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
exampleServletRegistration(), new FilterRegistrationBean(
new ExampleFilter()));
this.container.start();
assertThat(getResponse("http://localhost:8080/hello"), equalTo("[Hello World]"));
assertThat(getResponse(getLocalUrl("/hello")), equalTo("[Hello World]"));
}
@Test
@ -188,12 +189,14 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
@Test
public void specificPort() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
factory.setPort(8081);
int specificPort = SocketUtils.findAvailableTcpPort(40000);
factory.setPort(specificPort);
this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration());
this.container.start();
assertThat(getResponse("http://localhost:8081/hello"), equalTo("Hello World"));
assertEquals(8081, this.container.getPort());
assertThat(getResponse("http://localhost:" + specificPort + "/hello"),
equalTo("Hello World"));
assertEquals(specificPort, this.container.getPort());
}
@Test
@ -203,7 +206,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container = factory
.getEmbeddedServletContainer(exampleServletRegistration());
this.container.start();
assertThat(getResponse("http://localhost:8080/say/hello"), equalTo("Hello World"));
assertThat(getResponse(getLocalUrl("/say/hello")), equalTo("Hello World"));
}
@Test
@ -264,7 +267,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
factory.setDocumentRoot(this.temporaryFolder.getRoot());
this.container = factory.getEmbeddedServletContainer();
this.container.start();
assertThat(getResponse("http://localhost:8080/test.txt"), equalTo("test"));
assertThat(getResponse(getLocalUrl("/test.txt")), equalTo("test"));
}
@Test
@ -278,7 +281,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
factory.setMimeMappings(mimeMappings);
this.container = factory.getEmbeddedServletContainer();
this.container.start();
ClientHttpResponse response = getClientResponse("http://localhost:8080/test.xxcss");
ClientHttpResponse response = getClientResponse(getLocalUrl("/test.xxcss"));
assertThat(response.getHeaders().getContentType().toString(), equalTo("text/css"));
response.close();
}
@ -290,8 +293,12 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
this.container = factory.getEmbeddedServletContainer(
exampleServletRegistration(), errorServletRegistration());
this.container.start();
assertThat(getResponse("http://localhost:8080/hello"), equalTo("Hello World"));
assertThat(getResponse("http://localhost:8080/bang"), equalTo("Hello World"));
assertThat(getResponse(getLocalUrl("/hello")), equalTo("Hello World"));
assertThat(getResponse(getLocalUrl("/bang")), equalTo("Hello World"));
}
protected String getLocalUrl(String resourcePath) {
return "http://localhost:" + this.container.getPort() + resourcePath;
}
protected String getResponse(String url) throws IOException, URISyntaxException {

@ -26,6 +26,7 @@ import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletConta
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
@ -49,6 +50,7 @@ import static org.junit.Assert.assertThat;
* @author Phillip Webb
*/
public class EmbeddedServletContainerMvcIntegrationTests {
private AnnotationConfigEmbeddedWebApplicationContext context;
@After
@ -63,29 +65,30 @@ public class EmbeddedServletContainerMvcIntegrationTests {
@Test
public void tomcat() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
TomcatEmbeddedServletContainerFactory.class, Config.class);
doTest(this.context, "http://localhost:8080/hello");
TomcatConfig.class);
doTest(this.context, "/hello");
}
@Test
public void jetty() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
JettyEmbeddedServletContainerFactory.class, Config.class);
doTest(this.context, "http://localhost:8080/hello");
JettyConfig.class);
doTest(this.context, "/hello");
}
@Test
public void advancedConfig() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
AdvancedConfig.class);
doTest(this.context, "http://localhost:8081/example/spring/hello");
doTest(this.context, "/example/spring/hello");
}
private void doTest(AnnotationConfigEmbeddedWebApplicationContext context, String url)
throws Exception {
private void doTest(AnnotationConfigEmbeddedWebApplicationContext context,
String resourcePath) throws Exception {
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
ClientHttpRequest request = clientHttpRequestFactory.createRequest(new URI(url),
HttpMethod.GET);
ClientHttpRequest request = clientHttpRequestFactory.createRequest(new URI(
"http://localhost:" + context.getEmbeddedServletContainer().getPort()
+ resourcePath), HttpMethod.GET);
ClientHttpResponse response = request.execute();
try {
String actual = StreamUtils.copyToString(response.getBody(),
@ -97,6 +100,24 @@ public class EmbeddedServletContainerMvcIntegrationTests {
}
}
@Configuration
@Import(Config.class)
public static class TomcatConfig {
@Bean
public EmbeddedServletContainerFactory containerFactory() {
return new TomcatEmbeddedServletContainerFactory(0);
}
}
@Configuration
@Import(Config.class)
public static class JettyConfig {
@Bean
public EmbeddedServletContainerFactory containerFactory() {
return new JettyEmbeddedServletContainerFactory(0);
}
}
@Configuration
@EnableWebMvc
public static class Config {
@ -125,9 +146,9 @@ public class EmbeddedServletContainerMvcIntegrationTests {
@Bean
public EmbeddedServletContainerFactory containerFactory() {
JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();
factory.setPort(this.env.getProperty("port", Integer.class));
factory.setContextPath("/example");
JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(
0);
factory.setContextPath(this.env.getProperty("context"));
return factory;
}

@ -45,7 +45,7 @@ public class JettyEmbeddedServletContainerFactoryTests extends
@Override
protected JettyEmbeddedServletContainerFactory getFactory() {
return new JettyEmbeddedServletContainerFactory();
return new JettyEmbeddedServletContainerFactory(0);
}
@Test

@ -28,10 +28,10 @@ import org.apache.catalina.startup.Tomcat;
import org.junit.Test;
import org.mockito.InOrder;
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
import org.springframework.util.SocketUtils;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.inOrder;
@ -51,7 +51,7 @@ public class TomcatEmbeddedServletContainerFactoryTests extends
@Override
protected TomcatEmbeddedServletContainerFactory getFactory() {
return new TomcatEmbeddedServletContainerFactory();
return new TomcatEmbeddedServletContainerFactory(0);
}
// JMX MBean names clash if you get more than one Engine with the same name...
@ -59,12 +59,16 @@ public class TomcatEmbeddedServletContainerFactoryTests extends
public void tomcatEngineNames() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory();
this.container = factory.getEmbeddedServletContainer();
factory.setPort(8081);
factory.setPort(SocketUtils.findAvailableTcpPort(40000));
TomcatEmbeddedServletContainer container2 = (TomcatEmbeddedServletContainer) factory
.getEmbeddedServletContainer();
assertEquals("Tomcat", ((TomcatEmbeddedServletContainer) this.container)
.getTomcat().getEngine().getName());
assertEquals("Tomcat-1", container2.getTomcat().getEngine().getName());
// Make sure that the names are different
String firstContainerName = ((TomcatEmbeddedServletContainer) this.container)
.getTomcat().getEngine().getName();
String secondContainerName = container2.getTomcat().getEngine().getName();
assertFalse("Tomcat engines must have different names",
firstContainerName.equals(secondContainerName));
container2.stop();
}

Loading…
Cancel
Save