Skip to content

Commit 7bf3e35

Browse files
author
Dave Syer
committed
Remove some try/catch blocks from test methods
1 parent 4b1c58d commit 7bf3e35

File tree

2 files changed

+63
-84
lines changed

2 files changed

+63
-84
lines changed

spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmbeddedServletContainerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public interface EmbeddedServletContainerFactory {
4141
* @see EmbeddedServletContainer#stop()
4242
*/
4343
EmbeddedServletContainer getEmbdeddedServletContainer(
44-
ServletContextInitializer... initializers); //TODO(6/14/2013) Fix name of method
44+
ServletContextInitializer... initializers);
4545

4646
}

spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/web/MultipartAutoConfigurationTests.java

Lines changed: 62 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@
1515
*/
1616
package org.springframework.bootstrap.autoconfigure.web;
1717

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertNull;
20-
import static org.junit.Assert.assertSame;
21-
import static org.junit.Assert.fail;
22-
2318
import javax.servlet.MultipartConfigElement;
2419

20+
import org.junit.After;
21+
import org.junit.Rule;
2522
import org.junit.Test;
26-
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
23+
import org.junit.rules.ExpectedException;
2724
import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
2825
import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
2926
import org.springframework.bootstrap.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
@@ -38,40 +35,45 @@
3835
import org.springframework.web.servlet.DispatcherServlet;
3936
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
4037

38+
import static org.junit.Assert.assertEquals;
39+
import static org.junit.Assert.assertNull;
40+
import static org.junit.Assert.assertSame;
41+
4142
/**
4243
* A series of embedded unit tests, based on an empty configuration, no multipart
4344
* configuration, and a multipart configuration, with both Jetty and Tomcat.
4445
*
4546
* @author Greg Turnquist
47+
* @author Dave Syer
4648
*/
4749
public class MultipartAutoConfigurationTests {
48-
50+
4951
private AnnotationConfigEmbeddedWebApplicationContext context;
50-
52+
53+
@Rule
54+
public ExpectedException exception = ExpectedException.none();
55+
56+
@After
57+
public void close() {
58+
if (this.context != null) {
59+
this.context.close();
60+
}
61+
}
62+
5163
@Test
5264
public void containerWithNothing() {
5365
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
5466
ContainerWithNothing.class,
5567
EmbeddedServletContainerAutoConfiguration.class,
5668
MultipartAutoConfiguration.class);
57-
try {
58-
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
59-
assertNull(servlet.getMultipartResolver());
60-
try {
61-
this.context.getBean(StandardServletMultipartResolver.class);
62-
fail("Expected to receive a " + NoSuchBeanDefinitionException.class);
63-
} catch (NoSuchBeanDefinitionException e) {
64-
}
65-
try {
66-
this.context.getBean(MultipartResolver.class);
67-
fail("Expected to receive a " + NoSuchBeanDefinitionException.class);
68-
} catch (NoSuchBeanDefinitionException e) {
69-
}
70-
} finally {
71-
this.context.close();
72-
}
69+
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
70+
assertNull(servlet.getMultipartResolver());
71+
assertEquals(0,
72+
this.context.getBeansOfType(StandardServletMultipartResolver.class)
73+
.size());
74+
assertEquals(0, this.context.getBeansOfType(MultipartResolver.class).size());
7375
}
74-
76+
7577
@Configuration
7678
public static class ContainerWithNothing {
7779
}
@@ -82,23 +84,13 @@ public void containerWithNoMultipartJettyConfiguration() {
8284
ContainerWithNoMultipartJetty.class,
8385
EmbeddedServletContainerAutoConfiguration.class,
8486
MultipartAutoConfiguration.class);
85-
try {
86-
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
87-
assertNull(servlet.getMultipartResolver());
88-
try {
89-
this.context.getBean(StandardServletMultipartResolver.class);
90-
fail("Expected to receive a " + NoSuchBeanDefinitionException.class);
91-
} catch (NoSuchBeanDefinitionException e) {
92-
}
93-
try {
94-
this.context.getBean(MultipartResolver.class);
95-
fail("Expected to receive a " + NoSuchBeanDefinitionException.class);
96-
} catch (NoSuchBeanDefinitionException e) {
97-
}
98-
verifyServletWorks();
99-
} finally {
100-
this.context.close();
101-
}
87+
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
88+
assertNull(servlet.getMultipartResolver());
89+
assertEquals(0,
90+
this.context.getBeansOfType(StandardServletMultipartResolver.class)
91+
.size());
92+
assertEquals(0, this.context.getBeansOfType(MultipartResolver.class).size());
93+
verifyServletWorks();
10294
}
10395

10496
@Configuration
@@ -107,35 +99,26 @@ public static class ContainerWithNoMultipartJetty {
10799
JettyEmbeddedServletContainerFactory containerFactory() {
108100
return new JettyEmbeddedServletContainerFactory();
109101
}
102+
110103
@Bean
111104
WebController controller() {
112105
return new WebController();
113106
}
114-
}
107+
}
115108

116109
@Test
117110
public void containerWithNoMultipartTomcatConfiguration() {
118111
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
119112
ContainerWithNoMultipartTomcat.class,
120113
EmbeddedServletContainerAutoConfiguration.class,
121114
MultipartAutoConfiguration.class);
122-
try {
123-
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
124-
assertNull(servlet.getMultipartResolver());
125-
try {
126-
this.context.getBean(StandardServletMultipartResolver.class);
127-
fail("Expected to receive a " + NoSuchBeanDefinitionException.class);
128-
} catch (NoSuchBeanDefinitionException e) {
129-
}
130-
try {
131-
this.context.getBean(MultipartResolver.class);
132-
fail("Expected to receive a " + NoSuchBeanDefinitionException.class);
133-
} catch (NoSuchBeanDefinitionException e) {
134-
}
135-
verifyServletWorks();
136-
} finally {
137-
this.context.close();
138-
}
115+
DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class);
116+
assertNull(servlet.getMultipartResolver());
117+
assertEquals(0,
118+
this.context.getBeansOfType(StandardServletMultipartResolver.class)
119+
.size());
120+
assertEquals(0, this.context.getBeansOfType(MultipartResolver.class).size());
121+
verifyServletWorks();
139122
}
140123

141124
@Configuration
@@ -144,27 +127,23 @@ public static class ContainerWithNoMultipartTomcat {
144127
TomcatEmbeddedServletContainerFactory containerFactory() {
145128
return new TomcatEmbeddedServletContainerFactory();
146129
}
130+
147131
@Bean
148132
WebController controller() {
149133
return new WebController();
150134
}
151-
}
135+
}
152136

153137
@Test
154138
public void containerWithAutomatedMultipartJettyConfiguration() {
155139
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
156140
ContainerWithEverythingJetty.class,
157141
EmbeddedServletContainerAutoConfiguration.class,
158142
MultipartAutoConfiguration.class);
159-
try {
160-
this.context.getBean(MultipartConfigElement.class);
161-
assertSame(
162-
this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
163-
this.context.getBean(StandardServletMultipartResolver.class));
164-
verifyServletWorks();
165-
} finally {
166-
this.context.close();
167-
}
143+
this.context.getBean(MultipartConfigElement.class);
144+
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
145+
this.context.getBean(StandardServletMultipartResolver.class));
146+
verifyServletWorks();
168147
}
169148

170149
@Configuration
@@ -173,10 +152,12 @@ public static class ContainerWithEverythingJetty {
173152
MultipartConfigElement multipartConfigElement() {
174153
return new MultipartConfigElement("");
175154
}
155+
176156
@Bean
177157
JettyEmbeddedServletContainerFactory containerFactory() {
178158
return new JettyEmbeddedServletContainerFactory();
179159
}
160+
180161
@Bean
181162
WebController webController() {
182163
return new WebController();
@@ -189,15 +170,10 @@ public void containerWithAutomatedMultipartTomcatConfiguration() {
189170
ContainerWithEverythingTomcat.class,
190171
EmbeddedServletContainerAutoConfiguration.class,
191172
MultipartAutoConfiguration.class);
192-
try {
193-
this.context.getBean(MultipartConfigElement.class);
194-
assertSame(
195-
this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
196-
this.context.getBean(StandardServletMultipartResolver.class));
197-
verifyServletWorks();
198-
} finally {
199-
this.context.close();
200-
}
173+
this.context.getBean(MultipartConfigElement.class);
174+
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
175+
this.context.getBean(StandardServletMultipartResolver.class));
176+
verifyServletWorks();
201177
}
202178

203179
@Configuration
@@ -207,10 +183,12 @@ public static class ContainerWithEverythingTomcat {
207183
MultipartConfigElement multipartConfigElement() {
208184
return new MultipartConfigElement("");
209185
}
186+
210187
@Bean
211188
TomcatEmbeddedServletContainerFactory containerFactory() {
212189
return new TomcatEmbeddedServletContainerFactory();
213190
}
191+
214192
@Bean
215193
WebController webController() {
216194
return new WebController();
@@ -220,15 +198,16 @@ WebController webController() {
220198
@Controller
221199
public static class WebController {
222200
@RequestMapping("/")
223-
public @ResponseBody String index() {
201+
public @ResponseBody
202+
String index() {
224203
return "Hello";
225204
}
226205
}
227-
206+
228207
private void verifyServletWorks() {
229208
RestTemplate restTemplate = new RestTemplate();
230-
assertEquals(restTemplate.getForObject("http://localhost:8080/", String.class), "Hello");
209+
assertEquals(restTemplate.getForObject("http://localhost:8080/", String.class),
210+
"Hello");
231211
}
232212

233-
234213
}

0 commit comments

Comments
 (0)