Skip to content

Commit 28c1deb

Browse files
committed
Refactor WelcomePageIntegrationTests to use context runner
1 parent 19f375a commit 28c1deb

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

spring-boot-project/spring-boot-webmvc/src/test/java/org/springframework/boot/webmvc/autoconfigure/WelcomePageIntegrationTests.java

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020

2121
import org.junit.jupiter.api.Test;
2222

23+
import org.springframework.boot.autoconfigure.AutoConfigurations;
2324
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
24-
import org.springframework.boot.builder.SpringApplicationBuilder;
2525
import org.springframework.boot.http.converter.autoconfigure.HttpMessageConvertersAutoConfiguration;
26-
import org.springframework.boot.test.context.SpringBootTest;
27-
import org.springframework.boot.thymeleaf.autoconfigure.ThymeleafAutoConfiguration;
26+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
27+
import org.springframework.boot.testsupport.classpath.resources.WithResource;
2828
import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration;
29-
import org.springframework.boot.web.server.test.LocalServerPort;
29+
import org.springframework.boot.web.server.context.WebServerApplicationContext;
30+
import org.springframework.boot.web.server.servlet.context.AnnotationConfigServletWebServerApplicationContext;
3031
import org.springframework.boot.web.server.test.client.TestRestTemplate;
31-
import org.springframework.context.annotation.Configuration;
32-
import org.springframework.context.annotation.Import;
3332
import org.springframework.http.HttpStatus;
3433
import org.springframework.http.MediaType;
3534
import org.springframework.http.RequestEntity;
@@ -41,47 +40,43 @@
4140
* Integration tests for the welcome page.
4241
*
4342
* @author Madhura Bhave
43+
* @author Andy Wilkinson
4444
*/
45-
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
46-
"spring.web.resources.chain.strategy.content.enabled=true",
47-
"spring.thymeleaf.prefix=classpath:/org/springframework/boot/webmvc/autoconfigure/",
48-
"spring.web.resources.static-locations=classpath:/org/springframework/boot/webmvc/autoconfigure/static" })
45+
@WithResource(name = "static/index.html", content = "custom welcome page")
4946
class WelcomePageIntegrationTests {
5047

51-
@LocalServerPort
52-
private int port;
48+
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
49+
AnnotationConfigServletWebServerApplicationContext::new)
50+
.withPropertyValues("spring.web.resources.chain.strategy.content.enabled=true", "server.port=0")
51+
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
52+
WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
53+
TomcatServletWebServerAutoConfiguration.class, DispatcherServletAutoConfiguration.class));
5354

5455
private final TestRestTemplate template = new TestRestTemplate();
5556

5657
@Test
57-
void contentStrategyWithWelcomePage() throws Exception {
58-
RequestEntity<?> entity = RequestEntity.get(new URI("http://localhost:" + this.port + "/"))
59-
.header("Accept", MediaType.ALL.toString())
60-
.build();
61-
ResponseEntity<String> content = this.template.exchange(entity, String.class);
62-
assertThat(content.getBody()).contains("/custom-");
63-
assertThat(content.getStatusCode()).isEqualTo(HttpStatus.OK);
58+
void contentStrategyWithWelcomePage() {
59+
this.contextRunner.run((context) -> {
60+
int port = ((WebServerApplicationContext) context.getSourceApplicationContext()).getWebServer().getPort();
61+
RequestEntity<?> entity = RequestEntity.get(new URI("http://localhost:" + port + "/"))
62+
.header("Accept", MediaType.ALL.toString())
63+
.build();
64+
ResponseEntity<String> content = this.template.exchange(entity, String.class);
65+
assertThat(content.getBody()).contains("custom welcome page");
66+
assertThat(content.getStatusCode()).isEqualTo(HttpStatus.OK);
67+
});
6468
}
6569

6670
@Test
67-
void notAcceptableWelcomePage() throws Exception {
68-
RequestEntity<?> entity = RequestEntity.get(new URI("http://localhost:" + this.port + "/"))
69-
.header("Accept", "spring/boot")
70-
.build();
71-
ResponseEntity<String> content = this.template.exchange(entity, String.class);
72-
assertThat(content.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE);
73-
}
74-
75-
@Configuration
76-
@Import({ PropertyPlaceholderAutoConfiguration.class, WebMvcAutoConfiguration.class,
77-
HttpMessageConvertersAutoConfiguration.class, TomcatServletWebServerAutoConfiguration.class,
78-
DispatcherServletAutoConfiguration.class, ThymeleafAutoConfiguration.class })
79-
static class TestConfiguration {
80-
81-
static void main(String[] args) {
82-
new SpringApplicationBuilder(TestConfiguration.class).run(args);
83-
}
84-
71+
void notAcceptableWelcomePage() {
72+
this.contextRunner.run((context) -> {
73+
int port = ((WebServerApplicationContext) context.getSourceApplicationContext()).getWebServer().getPort();
74+
RequestEntity<?> entity = RequestEntity.get(new URI("http://localhost:" + port + "/"))
75+
.header("Accept", "spring/boot")
76+
.build();
77+
ResponseEntity<String> content = this.template.exchange(entity, String.class);
78+
assertThat(content.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE);
79+
});
8580
}
8681

8782
}

0 commit comments

Comments
 (0)