@@ -91,9 +91,7 @@ public void testAccessDeniedWhenAnonymous() throws Exception {
91
91
request .setContextPath ("/mycontext" );
92
92
request .setRequestURI ("/mycontext/secure/page.html" );
93
93
// 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 ("" ));
97
95
// Setup SecurityContextHolder, as filter needs to check if user is
98
96
// anonymous
99
97
SecurityContextHolder .getContext ()
@@ -119,9 +117,7 @@ public void testAccessDeniedWithRememberMe() throws Exception {
119
117
request .setContextPath ("/mycontext" );
120
118
request .setRequestURI ("/mycontext/secure/page.html" );
121
119
// 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 ("" ));
125
121
// Setup SecurityContextHolder, as filter needs to check if user is remembered
126
122
SecurityContext securityContext = SecurityContextHolder .createEmptyContext ();
127
123
securityContext .setAuthentication (
@@ -142,9 +138,7 @@ public void testAccessDeniedWhenNonAnonymous() throws Exception {
142
138
MockHttpServletRequest request = new MockHttpServletRequest ();
143
139
request .setServletPath ("/secure/page.html" );
144
140
// 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 ("" ));
148
142
// Setup SecurityContextHolder, as filter needs to check if user is
149
143
// anonymous
150
144
SecurityContextHolder .clearContext ();
@@ -167,9 +161,7 @@ public void testLocalizedErrorMessages() throws Exception {
167
161
MockHttpServletRequest request = new MockHttpServletRequest ();
168
162
request .setServletPath ("/secure/page.html" );
169
163
// 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 ("" ));
173
165
// Setup SecurityContextHolder, as filter needs to check if user is
174
166
// anonymous
175
167
SecurityContextHolder .getContext ()
@@ -198,9 +190,7 @@ public void redirectedToLoginFormAndSessionShowsOriginalTargetWhenAuthentication
198
190
request .setContextPath ("/mycontext" );
199
191
request .setRequestURI ("/mycontext/secure/page.html" );
200
192
// 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 ("" ));
204
194
// Test
205
195
RequestCache requestCache = new HttpSessionRequestCache ();
206
196
ExceptionTranslationFilter filter = new ExceptionTranslationFilter (this .mockEntryPoint , requestCache );
@@ -223,9 +213,7 @@ public void redirectedToLoginFormAndSessionShowsOriginalTargetWithExoticPortWhen
223
213
request .setContextPath ("/mycontext" );
224
214
request .setRequestURI ("/mycontext/secure/page.html" );
225
215
// 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 ("" ));
229
217
// Test
230
218
HttpSessionRequestCache requestCache = new HttpSessionRequestCache ();
231
219
ExceptionTranslationFilter filter = new ExceptionTranslationFilter (this .mockEntryPoint , requestCache );
@@ -265,8 +253,7 @@ public void thrownIOExceptionServletExceptionAndRuntimeExceptionsAreRethrown() t
265
253
filter .afterPropertiesSet ();
266
254
Exception [] exceptions = { new IOException (), new ServletException (), new RuntimeException () };
267
255
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 );
270
257
assertThatExceptionOfType (Exception .class )
271
258
.isThrownBy (() -> filter .doFilter (new MockHttpServletRequest (), new MockHttpServletResponse (), fc ))
272
259
.isSameAs (exception );
@@ -304,7 +291,11 @@ public void setMessageSourceWhenNotNullThenCanGet() {
304
291
filter .messages .getMessage (code );
305
292
verify (source ).getMessage (eq (code ), any (), any ());
306
293
}
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
+ }
308
299
private AuthenticationEntryPoint mockEntryPoint = (request , response , authException ) -> response
309
300
.sendRedirect (request .getContextPath () + "/login.jsp" );
310
301
0 commit comments