Skip to content

Commit 4375e59

Browse files
committed
Polish WebTestClient ApplicationContext tests
Prerequisite for gh-35042
1 parent 9ca7b62 commit 4375e59

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
package org.springframework.test.web.reactive.server.samples.bind;
1818

19-
import org.junit.jupiter.api.BeforeEach;
2019
import org.junit.jupiter.api.Test;
2120

22-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
23-
import org.springframework.context.annotation.Bean;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.ApplicationContext;
2423
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.annotation.Import;
25+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2526
import org.springframework.test.web.reactive.server.WebTestClient;
2627
import org.springframework.web.bind.annotation.GetMapping;
2728
import org.springframework.web.bind.annotation.RestController;
@@ -34,24 +35,18 @@
3435
* @author Rossen Stoyanchev
3536
* @since 5.0
3637
*/
37-
public class ApplicationContextTests {
38+
@SpringJUnitConfig
39+
class ApplicationContextTests {
3840

39-
private WebTestClient client;
41+
@Autowired
42+
ApplicationContext context;
4043

4144

42-
@BeforeEach
43-
public void setUp() throws Exception {
44-
45-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
46-
context.register(WebConfig.class);
47-
context.refresh();
48-
49-
this.client = WebTestClient.bindToApplicationContext(context).build();
50-
}
51-
5245
@Test
53-
public void test() throws Exception {
54-
this.client.get().uri("/test")
46+
void buildWithDefaults() {
47+
var client = WebTestClient.bindToApplicationContext(context).build();
48+
49+
client.get().uri("/test")
5550
.exchange()
5651
.expectStatus().isOk()
5752
.expectBody(String.class).isEqualTo("It works!");
@@ -60,20 +55,15 @@ public void test() throws Exception {
6055

6156
@Configuration
6257
@EnableWebFlux
63-
static class WebConfig {
64-
65-
@Bean
66-
public TestController controller() {
67-
return new TestController();
68-
}
69-
58+
@Import(TestController.class)
59+
static class WebFluxConfig {
7060
}
7161

7262
@RestController
7363
static class TestController {
7464

7565
@GetMapping("/test")
76-
public String handle() {
66+
String test() {
7767
return "It works!";
7868
}
7969
}

0 commit comments

Comments
 (0)