Skip to content

Commit af33cc2

Browse files
snicollPhillip Webb
authored andcommitted
Use random port in spring-boot tests
Update several tests to use random ports instead of hard coding '8080' or '8081'. fixes gh-607
1 parent fca7a8d commit af33cc2

File tree

6 files changed

+73
-41
lines changed

6 files changed

+73
-41
lines changed

spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ static class ExampleWebConfig {
541541

542542
@Bean
543543
public JettyEmbeddedServletContainerFactory container() {
544-
return new JettyEmbeddedServletContainerFactory();
544+
return new JettyEmbeddedServletContainerFactory(0);
545545
}
546546

547547
}

spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory;
4545
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
4646
import org.springframework.util.FileCopyUtils;
47+
import org.springframework.util.SocketUtils;
4748
import org.springframework.util.StreamUtils;
4849
import org.springframework.util.concurrent.ListenableFuture;
4950

5051
import static org.hamcrest.Matchers.equalTo;
52+
import static org.hamcrest.Matchers.lessThan;
5153
import static org.hamcrest.Matchers.notNullValue;
5254
import static org.junit.Assert.assertEquals;
5355
import static org.junit.Assert.assertThat;
@@ -88,18 +90,17 @@ public void startServlet() throws Exception {
8890
this.container = factory
8991
.getEmbeddedServletContainer(exampleServletRegistration());
9092
this.container.start();
91-
assertThat(getResponse("http://localhost:8080/hello"), equalTo("Hello World"));
93+
assertThat(getResponse(getLocalUrl("/hello")), equalTo("Hello World"));
9294
}
9395

9496
@Test
95-
public void emptyServerWhenPortIsZero() throws Exception {
97+
public void emptyServerWhenPortIsMinusOne() throws Exception {
9698
AbstractEmbeddedServletContainerFactory factory = getFactory();
97-
factory.setPort(0);
99+
factory.setPort(-1);
98100
this.container = factory
99101
.getEmbeddedServletContainer(exampleServletRegistration());
100102
this.container.start();
101-
this.thrown.expect(IOException.class);
102-
getResponse("http://localhost:8080/hello");
103+
assertThat(this.container.getPort(), lessThan(0)); // Jetty is -2
103104
}
104105

105106
@Test
@@ -110,7 +111,7 @@ public void stopServlet() throws Exception {
110111
this.container.start();
111112
this.container.stop();
112113
this.thrown.expect(IOException.class);
113-
getResponse("http://localhost:8080/hello");
114+
getResponse(getLocalUrl("/hello"));
114115
}
115116

116117
@Test
@@ -121,8 +122,8 @@ public void restartWithKeepAlive() throws Exception {
121122
this.container.start();
122123
HttpComponentsAsyncClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory();
123124
ListenableFuture<ClientHttpResponse> response1 = clientHttpRequestFactory
124-
.createAsyncRequest(new URI("http://localhost:8080/hello"),
125-
HttpMethod.GET).executeAsync();
125+
.createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET)
126+
.executeAsync();
126127
assertThat(response1.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200));
127128

128129
this.container.stop();
@@ -131,8 +132,8 @@ public void restartWithKeepAlive() throws Exception {
131132
this.container.start();
132133

133134
ListenableFuture<ClientHttpResponse> response2 = clientHttpRequestFactory
134-
.createAsyncRequest(new URI("http://localhost:8080/hello"),
135-
HttpMethod.GET).executeAsync();
135+
.createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET)
136+
.executeAsync();
136137
assertThat(response2.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200));
137138
}
138139

@@ -143,7 +144,7 @@ public void startServletAndFilter() throws Exception {
143144
exampleServletRegistration(), new FilterRegistrationBean(
144145
new ExampleFilter()));
145146
this.container.start();
146-
assertThat(getResponse("http://localhost:8080/hello"), equalTo("[Hello World]"));
147+
assertThat(getResponse(getLocalUrl("/hello")), equalTo("[Hello World]"));
147148
}
148149

149150
@Test
@@ -188,12 +189,14 @@ public void onStartup(ServletContext servletContext)
188189
@Test
189190
public void specificPort() throws Exception {
190191
AbstractEmbeddedServletContainerFactory factory = getFactory();
191-
factory.setPort(8081);
192+
int specificPort = SocketUtils.findAvailableTcpPort(40000);
193+
factory.setPort(specificPort);
192194
this.container = factory
193195
.getEmbeddedServletContainer(exampleServletRegistration());
194196
this.container.start();
195-
assertThat(getResponse("http://localhost:8081/hello"), equalTo("Hello World"));
196-
assertEquals(8081, this.container.getPort());
197+
assertThat(getResponse("http://localhost:" + specificPort + "/hello"),
198+
equalTo("Hello World"));
199+
assertEquals(specificPort, this.container.getPort());
197200
}
198201

199202
@Test
@@ -203,7 +206,7 @@ public void specificContextRoot() throws Exception {
203206
this.container = factory
204207
.getEmbeddedServletContainer(exampleServletRegistration());
205208
this.container.start();
206-
assertThat(getResponse("http://localhost:8080/say/hello"), equalTo("Hello World"));
209+
assertThat(getResponse(getLocalUrl("/say/hello")), equalTo("Hello World"));
207210
}
208211

209212
@Test
@@ -264,7 +267,7 @@ public void documentRoot() throws Exception {
264267
factory.setDocumentRoot(this.temporaryFolder.getRoot());
265268
this.container = factory.getEmbeddedServletContainer();
266269
this.container.start();
267-
assertThat(getResponse("http://localhost:8080/test.txt"), equalTo("test"));
270+
assertThat(getResponse(getLocalUrl("/test.txt")), equalTo("test"));
268271
}
269272

270273
@Test
@@ -278,7 +281,7 @@ public void mimeType() throws Exception {
278281
factory.setMimeMappings(mimeMappings);
279282
this.container = factory.getEmbeddedServletContainer();
280283
this.container.start();
281-
ClientHttpResponse response = getClientResponse("http://localhost:8080/test.xxcss");
284+
ClientHttpResponse response = getClientResponse(getLocalUrl("/test.xxcss"));
282285
assertThat(response.getHeaders().getContentType().toString(), equalTo("text/css"));
283286
response.close();
284287
}
@@ -290,8 +293,12 @@ public void errorPage() throws Exception {
290293
this.container = factory.getEmbeddedServletContainer(
291294
exampleServletRegistration(), errorServletRegistration());
292295
this.container.start();
293-
assertThat(getResponse("http://localhost:8080/hello"), equalTo("Hello World"));
294-
assertThat(getResponse("http://localhost:8080/bang"), equalTo("Hello World"));
296+
assertThat(getResponse(getLocalUrl("/hello")), equalTo("Hello World"));
297+
assertThat(getResponse(getLocalUrl("/bang")), equalTo("Hello World"));
298+
}
299+
300+
protected String getLocalUrl(String resourcePath) {
301+
return "http://localhost:" + this.container.getPort() + resourcePath;
295302
}
296303

297304
protected String getResponse(String url) throws IOException, URISyntaxException {

spring-boot/src/test/java/org/springframework/boot/context/embedded/EmbeddedServletContainerMvcIntegrationTests.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
2727
import org.springframework.context.annotation.Bean;
2828
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.context.annotation.Import;
2930
import org.springframework.context.annotation.PropertySource;
3031
import org.springframework.core.env.Environment;
3132
import org.springframework.http.HttpMethod;
@@ -49,6 +50,7 @@
4950
* @author Phillip Webb
5051
*/
5152
public class EmbeddedServletContainerMvcIntegrationTests {
53+
5254
private AnnotationConfigEmbeddedWebApplicationContext context;
5355

5456
@After
@@ -63,29 +65,30 @@ public void closeContext() {
6365
@Test
6466
public void tomcat() throws Exception {
6567
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
66-
TomcatEmbeddedServletContainerFactory.class, Config.class);
67-
doTest(this.context, "http://localhost:8080/hello");
68+
TomcatConfig.class);
69+
doTest(this.context, "/hello");
6870
}
6971

7072
@Test
7173
public void jetty() throws Exception {
7274
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
73-
JettyEmbeddedServletContainerFactory.class, Config.class);
74-
doTest(this.context, "http://localhost:8080/hello");
75+
JettyConfig.class);
76+
doTest(this.context, "/hello");
7577
}
7678

7779
@Test
7880
public void advancedConfig() throws Exception {
7981
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
8082
AdvancedConfig.class);
81-
doTest(this.context, "http://localhost:8081/example/spring/hello");
83+
doTest(this.context, "/example/spring/hello");
8284
}
8385

84-
private void doTest(AnnotationConfigEmbeddedWebApplicationContext context, String url)
85-
throws Exception {
86+
private void doTest(AnnotationConfigEmbeddedWebApplicationContext context,
87+
String resourcePath) throws Exception {
8688
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
87-
ClientHttpRequest request = clientHttpRequestFactory.createRequest(new URI(url),
88-
HttpMethod.GET);
89+
ClientHttpRequest request = clientHttpRequestFactory.createRequest(new URI(
90+
"http://localhost:" + context.getEmbeddedServletContainer().getPort()
91+
+ resourcePath), HttpMethod.GET);
8992
ClientHttpResponse response = request.execute();
9093
try {
9194
String actual = StreamUtils.copyToString(response.getBody(),
@@ -97,6 +100,24 @@ private void doTest(AnnotationConfigEmbeddedWebApplicationContext context, Strin
97100
}
98101
}
99102

103+
@Configuration
104+
@Import(Config.class)
105+
public static class TomcatConfig {
106+
@Bean
107+
public EmbeddedServletContainerFactory containerFactory() {
108+
return new TomcatEmbeddedServletContainerFactory(0);
109+
}
110+
}
111+
112+
@Configuration
113+
@Import(Config.class)
114+
public static class JettyConfig {
115+
@Bean
116+
public EmbeddedServletContainerFactory containerFactory() {
117+
return new JettyEmbeddedServletContainerFactory(0);
118+
}
119+
}
120+
100121
@Configuration
101122
@EnableWebMvc
102123
public static class Config {
@@ -125,9 +146,9 @@ public static class AdvancedConfig {
125146

126147
@Bean
127148
public EmbeddedServletContainerFactory containerFactory() {
128-
JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();
129-
factory.setPort(this.env.getProperty("port", Integer.class));
130-
factory.setContextPath("/example");
149+
JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(
150+
0);
151+
factory.setContextPath(this.env.getProperty("context"));
131152
return factory;
132153
}
133154

spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class JettyEmbeddedServletContainerFactoryTests extends
4545

4646
@Override
4747
protected JettyEmbeddedServletContainerFactory getFactory() {
48-
return new JettyEmbeddedServletContainerFactory();
48+
return new JettyEmbeddedServletContainerFactory(0);
4949
}
5050

5151
@Test

spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
import org.junit.Test;
2929
import org.mockito.InOrder;
3030
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactoryTests;
31+
import org.springframework.util.SocketUtils;
3132

3233
import static org.hamcrest.Matchers.equalTo;
33-
import static org.junit.Assert.assertEquals;
34-
import static org.junit.Assert.assertThat;
34+
import static org.junit.Assert.*;
3535
import static org.mockito.Matchers.any;
3636
import static org.mockito.Matchers.anyObject;
3737
import static org.mockito.Mockito.inOrder;
@@ -51,20 +51,24 @@ public class TomcatEmbeddedServletContainerFactoryTests extends
5151

5252
@Override
5353
protected TomcatEmbeddedServletContainerFactory getFactory() {
54-
return new TomcatEmbeddedServletContainerFactory();
54+
return new TomcatEmbeddedServletContainerFactory(0);
5555
}
5656

5757
// JMX MBean names clash if you get more than one Engine with the same name...
5858
@Test
5959
public void tomcatEngineNames() throws Exception {
6060
TomcatEmbeddedServletContainerFactory factory = getFactory();
6161
this.container = factory.getEmbeddedServletContainer();
62-
factory.setPort(8081);
62+
factory.setPort(SocketUtils.findAvailableTcpPort(40000));
6363
TomcatEmbeddedServletContainer container2 = (TomcatEmbeddedServletContainer) factory
6464
.getEmbeddedServletContainer();
65-
assertEquals("Tomcat", ((TomcatEmbeddedServletContainer) this.container)
66-
.getTomcat().getEngine().getName());
67-
assertEquals("Tomcat-1", container2.getTomcat().getEngine().getName());
65+
66+
// Make sure that the names are different
67+
String firstContainerName = ((TomcatEmbeddedServletContainer) this.container)
68+
.getTomcat().getEngine().getName();
69+
String secondContainerName = container2.getTomcat().getEngine().getName();
70+
assertFalse("Tomcat engines must have different names",
71+
firstContainerName.equals(secondContainerName));
6872
container2.stop();
6973
}
7074

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
port=8081
1+
context=/example

0 commit comments

Comments
 (0)