16
16
17
17
package org .springframework .boot .autoconfigure .web .reactive ;
18
18
19
- import org .hamcrest .Matchers ;
20
- import org .junit .Rule ;
21
19
import org .junit .Test ;
22
- import org .junit .rules .ExpectedException ;
23
20
import org .mockito .Mockito ;
24
21
22
+ import org .springframework .boot .autoconfigure .AutoConfigurations ;
23
+ import org .springframework .boot .test .context .runner .ReactiveWebApplicationContextRunner ;
24
+ import org .springframework .boot .web .embedded .tomcat .TomcatReactiveWebServerFactory ;
25
25
import org .springframework .boot .web .reactive .context .AnnotationConfigReactiveWebServerApplicationContext ;
26
26
import org .springframework .boot .web .reactive .server .ConfigurableReactiveWebServerFactory ;
27
27
import org .springframework .boot .web .reactive .server .ReactiveWebServerFactory ;
28
28
import org .springframework .boot .web .server .WebServerFactoryCustomizer ;
29
29
import org .springframework .context .ApplicationContextException ;
30
30
import org .springframework .context .annotation .Bean ;
31
31
import org .springframework .context .annotation .Configuration ;
32
- import org .springframework .context .annotation .Import ;
33
32
import org .springframework .http .server .reactive .HttpHandler ;
34
33
35
34
import static org .assertj .core .api .Assertions .assertThat ;
41
40
*/
42
41
public class ReactiveWebServerFactoryAutoConfigurationTests {
43
42
44
- private AnnotationConfigReactiveWebServerApplicationContext context ;
45
-
46
- @ Rule
47
- public ExpectedException thrown = ExpectedException .none ();
43
+ private ReactiveWebApplicationContextRunner contextRunner =
44
+ new ReactiveWebApplicationContextRunner (AnnotationConfigReactiveWebServerApplicationContext ::new )
45
+ .withConfiguration (AutoConfigurations .of (ReactiveWebServerFactoryAutoConfiguration .class ));
48
46
49
47
@ Test
50
48
public void createFromConfigClass () {
51
- this .context = new AnnotationConfigReactiveWebServerApplicationContext (
52
- BaseConfiguration .class );
53
- assertThat (this .context .getBeansOfType (ReactiveWebServerFactory .class ))
54
- .hasSize (1 );
55
- assertThat (this .context .getBeansOfType (WebServerFactoryCustomizer .class ))
56
- .hasSize (1 );
57
- assertThat (this .context
58
- .getBeansOfType (ReactiveWebServerFactoryCustomizer .class ))
59
- .hasSize (1 );
49
+ this .contextRunner
50
+ .withUserConfiguration (MockWebServerAutoConfiguration .class ,
51
+ HttpHandlerConfiguration .class )
52
+ .run (context -> {
53
+ assertThat (context .getBeansOfType (ReactiveWebServerFactory .class ))
54
+ .hasSize (1 );
55
+ assertThat (context .getBeansOfType (WebServerFactoryCustomizer .class ))
56
+ .hasSize (1 );
57
+ assertThat (context .getBeansOfType (ReactiveWebServerFactoryCustomizer .class ))
58
+ .hasSize (1 );
59
+ });
60
60
}
61
61
62
62
@ Test
63
63
public void missingHttpHandler () {
64
- this .thrown .expect (ApplicationContextException .class );
65
- this .thrown .expectMessage (Matchers .containsString ("missing HttpHandler bean" ));
66
- this .context = new AnnotationConfigReactiveWebServerApplicationContext (
67
- MissingHttpHandlerConfiguration .class );
64
+ this .contextRunner
65
+ .withUserConfiguration (MockWebServerAutoConfiguration .class )
66
+ .run (context -> {
67
+ assertThat (context .getStartupFailure ())
68
+ .isInstanceOf (ApplicationContextException .class )
69
+ .hasMessageContaining ("missing HttpHandler bean" );
70
+ });
68
71
}
69
72
70
73
@ Test
71
74
public void multipleHttpHandler () {
72
- this .thrown .expect (ApplicationContextException .class );
73
- this .thrown .expectMessage (Matchers .containsString (
74
- "multiple HttpHandler beans : httpHandler,additionalHttpHandler" ));
75
- this .context = new AnnotationConfigReactiveWebServerApplicationContext (
76
- BaseConfiguration .class , TooManyHttpHandlers .class );
75
+ this .contextRunner
76
+ .withUserConfiguration (MockWebServerAutoConfiguration .class ,
77
+ HttpHandlerConfiguration .class , TooManyHttpHandlers .class )
78
+ .run (context -> {
79
+ assertThat (context .getStartupFailure ())
80
+ .isInstanceOf (ApplicationContextException .class )
81
+ .hasMessageContaining ("multiple HttpHandler beans : " +
82
+ "httpHandler,additionalHttpHandler" );
83
+ });
77
84
}
78
85
79
86
@ Test
80
87
public void customizeReactiveWebServer () {
81
- this .context = new AnnotationConfigReactiveWebServerApplicationContext (
82
- BaseConfiguration .class , ReactiveWebServerCustomization .class );
83
- MockReactiveWebServerFactory factory = this .context
84
- .getBean (MockReactiveWebServerFactory .class );
85
- assertThat (factory .getPort ()).isEqualTo (9000 );
88
+ this .contextRunner
89
+ .withUserConfiguration (MockWebServerAutoConfiguration .class ,
90
+ HttpHandlerConfiguration .class , ReactiveWebServerCustomization .class )
91
+ .run (context -> {
92
+ assertThat (context .getBean (MockReactiveWebServerFactory .class ).getPort ())
93
+ .isEqualTo (9000 );
94
+ });
95
+ }
96
+
97
+ @ Test
98
+ public void defaultWebServerIsTomcat () {
99
+ // Tomcat should be chosen over Netty if the Tomcat library is present.
100
+ this .contextRunner
101
+ .withUserConfiguration (HttpHandlerConfiguration .class )
102
+ .run (context -> {
103
+ assertThat (context .getBean (ReactiveWebServerFactory .class ))
104
+ .isInstanceOf (TomcatReactiveWebServerFactory .class );
105
+ });
86
106
}
87
107
88
108
@ Configuration
89
- @ Import ({ MockWebServerAutoConfiguration .class ,
90
- ReactiveWebServerFactoryAutoConfiguration .class })
91
- protected static class BaseConfiguration {
109
+ protected static class HttpHandlerConfiguration {
92
110
93
111
@ Bean
94
112
public HttpHandler httpHandler () {
@@ -97,13 +115,6 @@ public HttpHandler httpHandler() {
97
115
98
116
}
99
117
100
- @ Configuration
101
- @ Import ({ MockWebServerAutoConfiguration .class ,
102
- ReactiveWebServerFactoryAutoConfiguration .class })
103
- protected static class MissingHttpHandlerConfiguration {
104
-
105
- }
106
-
107
118
@ Configuration
108
119
protected static class TooManyHttpHandlers {
109
120
0 commit comments