Skip to content

Commit b2873fb

Browse files
committed
Add WebMvcAutoConfigurationTests
[Fixes #53027833]
1 parent 9e14409 commit b2873fb

File tree

1 file changed

+94
-4
lines changed

1 file changed

+94
-4
lines changed

spring-autoconfigure/src/test/java/org/springframework/autoconfigure/web/WebMvcAutoConfigurationTests.java

+94-4
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,107 @@
1616

1717
package org.springframework.autoconfigure.web;
1818

19-
import org.junit.Ignore;
20-
import org.springframework.autoconfigure.web.WebMvcAutoConfiguration;
19+
import java.util.Map;
20+
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpServletResponse;
23+
24+
import org.junit.After;
25+
import org.junit.Rule;
26+
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
28+
import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
29+
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
30+
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
31+
import org.springframework.bootstrap.context.embedded.MockEmbeddedServletContainerFactory;
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.context.annotation.Configuration;
34+
import org.springframework.web.servlet.HandlerAdapter;
35+
import org.springframework.web.servlet.HandlerMapping;
36+
import org.springframework.web.servlet.View;
37+
import org.springframework.web.servlet.ViewResolver;
38+
import org.springframework.web.servlet.view.AbstractView;
39+
40+
import static org.junit.Assert.assertEquals;
2141

2242
/**
2343
* Tests for {@link WebMvcAutoConfiguration}.
2444
*
2545
* @author Phillip Webb
46+
* @author Dave Syer
2647
*/
27-
@Ignore
2848
public class WebMvcAutoConfigurationTests {
2949

30-
// FIXME
50+
private static final MockEmbeddedServletContainerFactory containerFactory = new MockEmbeddedServletContainerFactory();
51+
52+
@Rule
53+
public ExpectedException thrown = ExpectedException.none();
54+
55+
private AnnotationConfigEmbeddedWebApplicationContext context;
56+
57+
@After
58+
public void close() {
59+
if (this.context != null) {
60+
this.context.close();
61+
}
62+
}
63+
64+
@Test
65+
public void handerAdaptersCreated() throws Exception {
66+
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
67+
this.context.register(Config.class, WebMvcAutoConfiguration.class);
68+
this.context.refresh();
69+
assertEquals(3, this.context.getBeanNamesForType(HandlerAdapter.class).length);
70+
}
71+
72+
@Test
73+
public void handerMappingsCreated() throws Exception {
74+
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
75+
this.context.register(Config.class, WebMvcAutoConfiguration.class);
76+
this.context.refresh();
77+
assertEquals(6, this.context.getBeanNamesForType(HandlerMapping.class).length);
78+
}
79+
80+
@Test
81+
public void viewResolversCreatedIfViewsPresent() throws Exception {
82+
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
83+
this.context.register(Config.class, ViewConfig.class,
84+
WebMvcAutoConfiguration.class);
85+
this.context.refresh();
86+
assertEquals(2, this.context.getBeanNamesForType(ViewResolver.class).length);
87+
}
88+
89+
@Configuration
90+
protected static class ViewConfig {
91+
92+
@Bean
93+
public View jsonView() {
94+
return new AbstractView() {
95+
96+
@Override
97+
protected void renderMergedOutputModel(Map<String, Object> model,
98+
HttpServletRequest request, HttpServletResponse response)
99+
throws Exception {
100+
response.getOutputStream().write("Hello World".getBytes());
101+
}
102+
};
103+
}
104+
105+
}
106+
107+
@Configuration
108+
protected static class Config {
109+
110+
@Bean
111+
public EmbeddedServletContainerFactory containerFactory() {
112+
return containerFactory;
113+
}
114+
115+
@Bean
116+
public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() {
117+
return new EmbeddedServletContainerCustomizerBeanPostProcessor();
118+
}
119+
120+
}
31121

32122
}

0 commit comments

Comments
 (0)