Skip to content

Commit 56cf496

Browse files
committed
Add some tests for ServletContextApplicationContextInitializer
1 parent 6eb0449 commit 56cf496

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.context.web;
18+
19+
import javax.servlet.ServletContext;
20+
21+
import org.junit.Test;
22+
23+
import org.springframework.web.context.ConfigurableWebApplicationContext;
24+
import org.springframework.web.context.WebApplicationContext;
25+
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.times;
28+
import static org.mockito.Mockito.verify;
29+
30+
/**
31+
* Tests for {@link ServletContextApplicationContextInitializer}.
32+
*
33+
* @author Andy Wilkinson
34+
*/
35+
public class ServletContextApplicationContextInitializerTests {
36+
37+
private final ServletContext servletContext = mock(ServletContext.class);
38+
39+
private final ConfigurableWebApplicationContext applicationContext = mock(
40+
ConfigurableWebApplicationContext.class);
41+
42+
@Test
43+
public void servletContextIsSetOnTheApplicationContext() {
44+
new ServletContextApplicationContextInitializer(this.servletContext)
45+
.initialize(this.applicationContext);
46+
verify(this.applicationContext).setServletContext(this.servletContext);
47+
}
48+
49+
@Test
50+
public void applicationContextIsNotStoredInServletContextByDefault() {
51+
new ServletContextApplicationContextInitializer(this.servletContext)
52+
.initialize(this.applicationContext);
53+
verify(this.servletContext, times(0)).setAttribute(
54+
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
55+
this.applicationContext);
56+
}
57+
58+
@Test
59+
public void applicationContextCanBeStoredInServletContext() {
60+
new ServletContextApplicationContextInitializer(this.servletContext, true)
61+
.initialize(this.applicationContext);
62+
verify(this.servletContext).setAttribute(
63+
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
64+
this.applicationContext);
65+
}
66+
67+
}

0 commit comments

Comments
 (0)