Skip to content

Commit 16729ff

Browse files
committed
Refactor WebMvc auto-config tests and add base test
Introduce AbstractWebMvcAutoConfigurationTest to centralize MockMvc setup and common endpoint assertions. Add WebMvcAutoConfigurationAllDisabledTest for coverage when features are disabled. Update WebMvcAutoConfigurationAllEnabledTest and WebMvcAutoConfigurationTest to extend the new base, remove duplicated setup/annotations and unused imports, and simplify assertions (adjusted content-negotiation expectations and view-resolver property). These changes reduce test duplication and consolidate shared test logic.
1 parent 4817f56 commit 16729ff

4 files changed

Lines changed: 280 additions & 22 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.spring.boot.webmvc.autoconfigure;
19+
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import io.microsphere.spring.test.domain.User;
22+
import io.microsphere.spring.test.web.controller.TestController;
23+
import io.microsphere.spring.test.webmvc.RouterFunctionTestConfig;
24+
import jakarta.servlet.ServletException;
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Disabled;
27+
import org.junit.jupiter.api.Test;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
30+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
31+
import org.springframework.test.context.web.WebAppConfiguration;
32+
import org.springframework.test.web.servlet.MockMvc;
33+
import org.springframework.test.web.servlet.RequestBuilder;
34+
import org.springframework.web.context.ConfigurableWebApplicationContext;
35+
36+
import static org.junit.jupiter.api.Assertions.assertThrows;
37+
import static org.springframework.http.MediaType.APPLICATION_JSON;
38+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
39+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
40+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
41+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
42+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
43+
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
44+
45+
/**
46+
* Abstract class of {@link WebMvcAutoConfiguration} test
47+
*
48+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
49+
* @see WebMvcAutoConfiguration
50+
* @since 1.0.0
51+
*/
52+
@Disabled
53+
@WebAppConfiguration
54+
@SpringJUnitConfig(classes = {
55+
TestController.class, // Test Controller
56+
RouterFunctionTestConfig.class // Test RouterFunction
57+
})
58+
@EnableAutoConfiguration
59+
class AbstractWebMvcAutoConfigurationTest {
60+
61+
@Autowired
62+
protected ConfigurableWebApplicationContext context;
63+
64+
@Autowired
65+
protected TestController testController;
66+
67+
protected MockMvc mockMvc;
68+
69+
@BeforeEach
70+
void setUp() {
71+
this.mockMvc = webAppContextSetup(this.context).build();
72+
}
73+
74+
@Test
75+
void test() throws Exception {
76+
testWebEndpoints();
77+
}
78+
79+
/**
80+
* Test the Web Endpoints
81+
*
82+
* @see #testHelloWorld()
83+
* @see #testGreeting()
84+
* @see #testUser()
85+
* @see #testError()
86+
* @see #testResponseEntity()
87+
* @see #testUpdatePerson()
88+
*/
89+
protected void testWebEndpoints() throws Exception {
90+
this.testHelloWorld();
91+
this.testGreeting();
92+
this.testUser();
93+
this.testError();
94+
this.testResponseEntity();
95+
this.testUpdatePerson();
96+
}
97+
98+
/**
99+
* Test {@link TestController#helloWorld()}
100+
*
101+
* @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
102+
*/
103+
protected void testHelloWorld() throws Exception {
104+
this.mockMvc.perform(get("/test/helloworld"))
105+
.andExpect(status().isOk())
106+
.andExpect(content().string(this.testController.helloWorld()));
107+
}
108+
109+
/**
110+
* Test {@link TestController#helloWorld()}
111+
*
112+
* @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
113+
*/
114+
protected void testGreeting() throws Exception {
115+
String pattern = "/test/greeting/{message}";
116+
String message = "Mercy";
117+
this.mockMvc.perform(get(pattern, message))
118+
.andExpect(status().isOk())
119+
.andExpect(content().string(this.testController.greeting(message)));
120+
}
121+
122+
/**
123+
* Test {@link TestController#helloWorld()}
124+
*
125+
* @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
126+
*/
127+
protected void testUser() throws Exception {
128+
ObjectMapper objectMapper = new ObjectMapper();
129+
User user = new User();
130+
user.setName("Mercy");
131+
user.setAge(18);
132+
String json = objectMapper.writeValueAsString(user);
133+
this.mockMvc.perform(post("/test/user")
134+
.contentType(APPLICATION_JSON)
135+
.content(json))
136+
.andExpect(status().isOk())
137+
.andExpect(content().string(json));
138+
}
139+
140+
/**
141+
* Test {@link TestController#helloWorld()}
142+
*
143+
* @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
144+
*/
145+
protected void testError() {
146+
assertThrows(ServletException.class, () -> this.mockMvc.perform(get("/test/error")
147+
.param("message", "For testing")).andReturn());
148+
}
149+
150+
/**
151+
* Test {@link TestController#helloWorld()}
152+
*
153+
* @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
154+
*/
155+
protected void testResponseEntity() throws Exception {
156+
this.mockMvc.perform(put("/test/response-entity"))
157+
.andExpect(status().isOk())
158+
.andExpect(content().string(this.testController.responseEntity().getBody()));
159+
}
160+
161+
/**
162+
* Test {@link TestController#helloWorld()}
163+
*
164+
* @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
165+
*/
166+
protected void testView() throws Exception {
167+
this.mockMvc.perform(get("/test/view"))
168+
.andExpect(status().isOk())
169+
.andExpect(content().string(""));
170+
}
171+
172+
/**
173+
* Test RouterFunctionTestConfig#nestedPersonRouterFunction(PersonHandler)
174+
*
175+
* @throws Exception If failed to execute {@link MockMvc#perform(RequestBuilder)}
176+
*/
177+
protected void testUpdatePerson() throws Exception {
178+
this.mockMvc.perform(put("/test/person/{id}", "1"))
179+
.andExpect(status().isOk());
180+
}
181+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.spring.boot.webmvc.autoconfigure;
19+
20+
21+
import io.microsphere.spring.boot.webmvc.autoconfigure.WebMvcAutoConfiguration.LoggingConfiguration;
22+
import io.microsphere.spring.web.servlet.filter.ContentCachingFilter;
23+
import io.microsphere.spring.webmvc.annotation.WebMvcExtensionConfiguration;
24+
import io.microsphere.spring.webmvc.config.ConfigurableContentNegotiationManagerWebMvcConfigurer;
25+
import io.microsphere.spring.webmvc.context.ExclusiveViewResolverApplicationListener;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Test;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.boot.test.context.SpringBootTest;
30+
import org.springframework.test.context.TestPropertySource;
31+
import org.springframework.web.accept.ContentNegotiationManager;
32+
import org.springframework.web.accept.ContentNegotiationStrategy;
33+
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
34+
35+
import java.util.List;
36+
37+
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
import static org.junit.jupiter.api.Assertions.assertTrue;
39+
40+
/**
41+
* {@link WebMvcAutoConfiguration} Test with all enabled components
42+
*
43+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
44+
* @see WebMvcAutoConfiguration
45+
* @since 1.0.0
46+
*/
47+
@SpringBootTest(classes = {
48+
WebMvcAutoConfigurationAllDisabledTest.class
49+
})
50+
@TestPropertySource(
51+
properties = {
52+
"microsphere.spring.webmvc.content-negotiation.enabled=false",
53+
"microsphere.spring.webmvc.filter.enabled=false",
54+
"microsphere.spring.webmvc.logging.enabled=false"
55+
}
56+
)
57+
class WebMvcAutoConfigurationAllDisabledTest extends AbstractWebMvcAutoConfigurationTest {
58+
59+
@Autowired(required = false)
60+
private WebMvcExtensionConfiguration webMvcExtensionConfiguration;
61+
62+
@Autowired(required = false)
63+
private ContentCachingFilter contentCachingFilter;
64+
65+
@Autowired(required = false)
66+
private ConfigurableContentNegotiationManagerWebMvcConfigurer webMvcConfigurer;
67+
68+
@Autowired
69+
private ContentNegotiationManager contentNegotiationManager;
70+
71+
@Autowired(required = false)
72+
private LoggingConfiguration loggingConfiguration;
73+
74+
@Autowired(required = false)
75+
private ExclusiveViewResolverApplicationListener listener;
76+
77+
@Override
78+
@BeforeEach
79+
public void setUp() {
80+
super.setUp();
81+
}
82+
83+
@Test
84+
void test() throws Exception {
85+
assertContentNegotiationManager(this.contentNegotiationManager);
86+
}
87+
88+
void assertContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) {
89+
List<ContentNegotiationStrategy> strategies = contentNegotiationManager.getStrategies();
90+
assertEquals(1, strategies.size());
91+
92+
ContentNegotiationStrategy strategy = strategies.get(0);
93+
assertTrue(strategy instanceof HeaderContentNegotiationStrategy);
94+
}
95+
}

microsphere-spring-boot-webmvc/src/test/java/io/microsphere/spring/boot/webmvc/autoconfigure/WebMvcAutoConfigurationAllEnabledTest.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,20 @@
1919

2020

2121
import io.microsphere.spring.boot.webmvc.autoconfigure.WebMvcAutoConfiguration.LoggingConfiguration;
22-
import io.microsphere.spring.test.webmvc.AbstractWebMvcTest;
2322
import io.microsphere.spring.web.servlet.filter.ContentCachingFilter;
2423
import io.microsphere.spring.webmvc.annotation.WebMvcExtensionConfiguration;
2524
import io.microsphere.spring.webmvc.config.ConfigurableContentNegotiationManagerWebMvcConfigurer;
2625
import io.microsphere.spring.webmvc.context.ExclusiveViewResolverApplicationListener;
2726
import org.junit.jupiter.api.BeforeEach;
2827
import org.junit.jupiter.api.Test;
2928
import org.springframework.beans.factory.annotation.Autowired;
30-
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3129
import org.springframework.boot.test.context.SpringBootTest;
32-
import org.springframework.http.MediaType;
3330
import org.springframework.test.context.TestPropertySource;
3431
import org.springframework.web.accept.ContentNegotiationManager;
3532
import org.springframework.web.accept.ContentNegotiationStrategy;
3633
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
37-
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
3834

3935
import java.util.List;
40-
import java.util.Map;
4136

4237
import static org.junit.jupiter.api.Assertions.assertEquals;
4338
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -54,7 +49,7 @@
5449
})
5550
@TestPropertySource(
5651
properties = {
57-
"microsphere.spring.webmvc.view-resolver.exclusive-bean-name=mvcViewResolver",
52+
"microsphere.spring.webmvc.view-resolver.exclusive-bean-name=viewResolver",
5853
"microsphere.spring.webmvc.content-negotiation.enabled=true",
5954
"microsphere.spring.webmvc.content-negotiation.favorParameter=true",
6055
"microsphere.spring.webmvc.content-negotiation.parameterName=p",
@@ -66,8 +61,7 @@
6661
"microsphere.spring.webmvc.logging.enabled=true"
6762
}
6863
)
69-
@EnableAutoConfiguration
70-
class WebMvcAutoConfigurationAllEnabledTest extends AbstractWebMvcTest {
64+
class WebMvcAutoConfigurationAllEnabledTest extends AbstractWebMvcAutoConfigurationTest {
7165

7266
@Autowired
7367
private WebMvcExtensionConfiguration webMvcExtensionConfiguration;
@@ -111,17 +105,10 @@ void assertContentNegotiationManager(ContentNegotiationManager contentNegotiatio
111105
ContentNegotiationStrategy strategy1 = strategies.get(0);
112106
ContentNegotiationStrategy strategy2 = strategies.get(1);
113107

114-
assertTrue(strategy1 instanceof PathExtensionContentNegotiationStrategy);
115108
assertTrue(strategy2 instanceof ParameterContentNegotiationStrategy);
116109

117-
PathExtensionContentNegotiationStrategy pathExtensionContentNegotiationStrategy = (PathExtensionContentNegotiationStrategy) strategy1;
118-
Map<String, MediaType> mediaTypes = pathExtensionContentNegotiationStrategy.getMediaTypes();
119-
assertEquals(2, mediaTypes.size());
120-
121110
ParameterContentNegotiationStrategy parameterContentNegotiationStrategy = (ParameterContentNegotiationStrategy) strategy2;
122111

123112
assertEquals("p", parameterContentNegotiationStrategy.getParameterName());
124-
assertEquals(false, pathExtensionContentNegotiationStrategy.isIgnoreUnknownExtensions());
125-
assertEquals(true, pathExtensionContentNegotiationStrategy.isUseRegisteredExtensionsOnly());
126113
}
127114
}

microsphere-spring-boot-webmvc/src/test/java/io/microsphere/spring/boot/webmvc/autoconfigure/WebMvcAutoConfigurationTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
package io.microsphere.spring.boot.webmvc.autoconfigure;
1919

2020

21-
import io.microsphere.spring.test.webmvc.AbstractWebMvcTest;
2221
import io.microsphere.spring.web.servlet.filter.ContentCachingFilter;
2322
import io.microsphere.spring.webmvc.annotation.WebMvcExtensionConfiguration;
2423
import io.microsphere.spring.webmvc.config.ConfigurableContentNegotiationManagerWebMvcConfigurer;
2524
import io.microsphere.spring.webmvc.context.ExclusiveViewResolverApplicationListener;
2625
import org.junit.jupiter.api.Test;
2726
import org.springframework.beans.factory.annotation.Autowired;
28-
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2927
import org.springframework.boot.test.context.SpringBootTest;
3028

3129
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -41,8 +39,7 @@
4139
@SpringBootTest(classes = {
4240
WebMvcAutoConfigurationTest.class
4341
})
44-
@EnableAutoConfiguration
45-
class WebMvcAutoConfigurationTest extends AbstractWebMvcTest {
42+
class WebMvcAutoConfigurationTest extends AbstractWebMvcAutoConfigurationTest {
4643

4744
@Autowired
4845
private WebMvcExtensionConfiguration webMvcExtensionConfiguration;
@@ -66,8 +63,6 @@ void test() throws Exception {
6663
assertNotNull(this.webMvcConfigurer);
6764
assertNotNull(this.loggingConfiguration);
6865
assertNull(this.listener);
69-
70-
this.testHelloWorld();
71-
this.testGreeting();
66+
super.test();
7267
}
7368
}

0 commit comments

Comments
 (0)