Skip to content

Commit 25d13ac

Browse files
committed
Sensible defaults for Servlet & Filter registrations in mock
Prior to this commit, the getter methods in MockServletContext threw an UnsupportedOperationException when trying to retrieve Servlet and Filter registrations. This commit improves the behavior of these methods by returning null when a single registration is requested and an empty map when all registrations are requested. This is now in line with the Javadoc for ServletContext. Note, however, that the corresponding setter methods still throw UnsupportedOperationExceptions which is suitable behavior for a mock. Issue: SPR-12290
1 parent c0dedec commit 25d13ac

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockServletContext.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
* through {@link #setMajorVersion}/{@link #setMinorVersion}; default is 3.0.
6464
* Note that Servlet 3.0 support is limited: servlet, filter and listener
6565
* registration methods are not supported; neither is JSP configuration.
66-
* We generally do not recommend to unit-test your ServletContainerInitializers and
66+
* We generally do not recommend to unit test your ServletContainerInitializers and
6767
* WebApplicationInitializers which is where those registration methods would be used.
6868
*
6969
* <p>Used for testing the Spring web framework; only rarely necessary for testing
@@ -605,14 +605,22 @@ public <T extends Servlet> T createServlet(Class<T> c) throws ServletException {
605605
throw new UnsupportedOperationException();
606606
}
607607

608+
/**
609+
* This method always returns {@code null}.
610+
* @see javax.servlet.ServletContext#getServletRegistration(java.lang.String)
611+
*/
608612
@Override
609613
public ServletRegistration getServletRegistration(String servletName) {
610-
throw new UnsupportedOperationException();
614+
return null;
611615
}
612616

617+
/**
618+
* This method always returns an {@linkplain Collections#emptyMap empty map}.
619+
* @see javax.servlet.ServletContext#getServletRegistrations()
620+
*/
613621
@Override
614622
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
615-
throw new UnsupportedOperationException();
623+
return Collections.emptyMap();
616624
}
617625

618626
@Override
@@ -635,14 +643,22 @@ public <T extends Filter> T createFilter(Class<T> c) throws ServletException {
635643
throw new UnsupportedOperationException();
636644
}
637645

646+
/**
647+
* This method always returns {@code null}.
648+
* @see javax.servlet.ServletContext#getFilterRegistration(java.lang.String)
649+
*/
638650
@Override
639651
public FilterRegistration getFilterRegistration(String filterName) {
640-
throw new UnsupportedOperationException();
652+
return null;
641653
}
642654

655+
/**
656+
* This method always returns an {@linkplain Collections#emptyMap empty map}.
657+
* @see javax.servlet.ServletContext#getFilterRegistrations()
658+
*/
643659
@Override
644660
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
645-
throw new UnsupportedOperationException();
661+
return Collections.emptyMap();
646662
}
647663

648664
@Override

spring-test/src/test/java/org/springframework/mock/web/MockServletContextTests.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616

1717
package org.springframework.mock.web;
1818

19+
import java.util.Map;
1920
import java.util.Set;
2021

2122
import javax.activation.FileTypeMap;
2223
import javax.activation.MimetypesFileTypeMap;
24+
import javax.servlet.FilterRegistration;
2325
import javax.servlet.RequestDispatcher;
26+
import javax.servlet.ServletRegistration;
2427

2528
import org.junit.Test;
2629

2730
import static org.hamcrest.CoreMatchers.*;
28-
2931
import static org.junit.Assert.*;
3032

3133
/**
@@ -147,4 +149,40 @@ public void setDefaultServletName() throws Exception {
147149
assertEquals(newDefault, response.getForwardedUrl());
148150
}
149151

152+
/**
153+
* @since 4.1.2
154+
*/
155+
@Test
156+
public void getServletRegistration() {
157+
assertNull(sc.getServletRegistration("servlet"));
158+
}
159+
160+
/**
161+
* @since 4.1.2
162+
*/
163+
@Test
164+
public void getServletRegistrations() {
165+
Map<String, ? extends ServletRegistration> servletRegistrations = sc.getServletRegistrations();
166+
assertNotNull(servletRegistrations);
167+
assertEquals(0, servletRegistrations.size());
168+
}
169+
170+
/**
171+
* @since 4.1.2
172+
*/
173+
@Test
174+
public void getFilterRegistration() {
175+
assertNull(sc.getFilterRegistration("filter"));
176+
}
177+
178+
/**
179+
* @since 4.1.2
180+
*/
181+
@Test
182+
public void getFilterRegistrations() {
183+
Map<String, ? extends FilterRegistration> filterRegistrations = sc.getFilterRegistrations();
184+
assertNotNull(filterRegistrations);
185+
assertEquals(0, filterRegistrations.size());
186+
}
187+
150188
}

0 commit comments

Comments
 (0)