Skip to content

Commit 17e09f9

Browse files
committed
Merged changes from Example 4: Avoid duplicate creation of mocks in ExceptionTranslationFilterTests.java
1 parent 33df5b0 commit 17e09f9

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

web/src/test/java/org/springframework/security/web/access/ExceptionTranslationFilterTests.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ public void testAccessDeniedWhenAnonymous() throws Exception {
9191
request.setContextPath("/mycontext");
9292
request.setRequestURI("/mycontext/secure/page.html");
9393
// Setup the FilterChain to thrown an access denied exception
94-
FilterChain fc = mock(FilterChain.class);
95-
willThrow(new AccessDeniedException("")).given(fc)
96-
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
94+
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
9795
// Setup SecurityContextHolder, as filter needs to check if user is
9896
// anonymous
9997
SecurityContextHolder.getContext()
@@ -119,9 +117,7 @@ public void testAccessDeniedWithRememberMe() throws Exception {
119117
request.setContextPath("/mycontext");
120118
request.setRequestURI("/mycontext/secure/page.html");
121119
// Setup the FilterChain to thrown an access denied exception
122-
FilterChain fc = mock(FilterChain.class);
123-
willThrow(new AccessDeniedException("")).given(fc)
124-
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
120+
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
125121
// Setup SecurityContextHolder, as filter needs to check if user is remembered
126122
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
127123
securityContext.setAuthentication(
@@ -142,9 +138,7 @@ public void testAccessDeniedWhenNonAnonymous() throws Exception {
142138
MockHttpServletRequest request = new MockHttpServletRequest();
143139
request.setServletPath("/secure/page.html");
144140
// Setup the FilterChain to thrown an access denied exception
145-
FilterChain fc = mock(FilterChain.class);
146-
willThrow(new AccessDeniedException("")).given(fc)
147-
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
141+
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
148142
// Setup SecurityContextHolder, as filter needs to check if user is
149143
// anonymous
150144
SecurityContextHolder.clearContext();
@@ -167,9 +161,7 @@ public void testLocalizedErrorMessages() throws Exception {
167161
MockHttpServletRequest request = new MockHttpServletRequest();
168162
request.setServletPath("/secure/page.html");
169163
// Setup the FilterChain to thrown an access denied exception
170-
FilterChain fc = mock(FilterChain.class);
171-
willThrow(new AccessDeniedException("")).given(fc)
172-
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
164+
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
173165
// Setup SecurityContextHolder, as filter needs to check if user is
174166
// anonymous
175167
SecurityContextHolder.getContext()
@@ -198,9 +190,7 @@ public void redirectedToLoginFormAndSessionShowsOriginalTargetWhenAuthentication
198190
request.setContextPath("/mycontext");
199191
request.setRequestURI("/mycontext/secure/page.html");
200192
// Setup the FilterChain to thrown an authentication failure exception
201-
FilterChain fc = mock(FilterChain.class);
202-
willThrow(new BadCredentialsException("")).given(fc)
203-
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
193+
FilterChain fc = mockFilterChainWiehException(new BadCredentialsException(""));
204194
// Test
205195
RequestCache requestCache = new HttpSessionRequestCache();
206196
ExceptionTranslationFilter filter = new ExceptionTranslationFilter(this.mockEntryPoint, requestCache);
@@ -223,9 +213,7 @@ public void redirectedToLoginFormAndSessionShowsOriginalTargetWithExoticPortWhen
223213
request.setContextPath("/mycontext");
224214
request.setRequestURI("/mycontext/secure/page.html");
225215
// Setup the FilterChain to thrown an authentication failure exception
226-
FilterChain fc = mock(FilterChain.class);
227-
willThrow(new BadCredentialsException("")).given(fc)
228-
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
216+
FilterChain fc = mockFilterChainWiehException(new BadCredentialsException(""));
229217
// Test
230218
HttpSessionRequestCache requestCache = new HttpSessionRequestCache();
231219
ExceptionTranslationFilter filter = new ExceptionTranslationFilter(this.mockEntryPoint, requestCache);
@@ -265,8 +253,7 @@ public void thrownIOExceptionServletExceptionAndRuntimeExceptionsAreRethrown() t
265253
filter.afterPropertiesSet();
266254
Exception[] exceptions = { new IOException(), new ServletException(), new RuntimeException() };
267255
for (Exception exception : exceptions) {
268-
FilterChain fc = mock(FilterChain.class);
269-
willThrow(exception).given(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
256+
FilterChain fc = mockFilterChainWiehException(exception);
270257
assertThatExceptionOfType(Exception.class)
271258
.isThrownBy(() -> filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), fc))
272259
.isSameAs(exception);
@@ -304,7 +291,11 @@ public void setMessageSourceWhenNotNullThenCanGet() {
304291
filter.messages.getMessage(code);
305292
verify(source).getMessage(eq(code), any(), any());
306293
}
307-
294+
private FilterChain mockFilterChainWiehException(Object exception) throws ServletException, IOException {
295+
FilterChain fc = mock(FilterChain.class);
296+
willThrow((Throwable) exception).given(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
297+
return fc;
298+
}
308299
private AuthenticationEntryPoint mockEntryPoint = (request, response, authException) -> response
309300
.sendRedirect(request.getContextPath() + "/login.jsp");
310301

0 commit comments

Comments
 (0)