1
+ package com .baeldung .apikeyauthentication .configuration ;
2
+
3
+ import org .junit .Before ;
4
+ import org .junit .Test ;
5
+ import org .junit .runner .RunWith ;
6
+ import org .mockito .InjectMocks ;
7
+ import org .mockito .Mock ;
8
+ import org .mockito .MockedStatic ;
9
+ import org .mockito .Mockito ;
10
+ import org .mockito .MockitoAnnotations ;
11
+ import org .mockito .junit .MockitoJUnitRunner ;
12
+ import org .springframework .http .MediaType ;
13
+ import org .springframework .security .core .Authentication ;
14
+ import org .springframework .security .core .context .SecurityContextHolder ;
15
+ import jakarta .servlet .FilterChain ;
16
+ import jakarta .servlet .ServletException ;
17
+ import jakarta .servlet .http .HttpServletRequest ;
18
+ import jakarta .servlet .http .HttpServletResponse ;
19
+ import java .io .IOException ;
20
+ import java .io .PrintWriter ;
21
+ import static org .mockito .Mockito .*;
22
+
23
+
24
+ @ RunWith (MockitoJUnitRunner .class )
25
+ public class AuthenticationFilterUnitTest {
26
+
27
+ @ Mock
28
+ private HttpServletRequest request ;
29
+
30
+ @ Mock
31
+ private HttpServletResponse response ;
32
+
33
+ @ Mock
34
+ private FilterChain filterChain ;
35
+
36
+ @ Mock
37
+ private Authentication authentication ;
38
+
39
+ @ InjectMocks
40
+ private AuthenticationFilter authenticationFilter ;
41
+
42
+ @ Before
43
+ public void setUp () {
44
+ MockitoAnnotations .openMocks (this );
45
+ }
46
+
47
+ @ Test
48
+ public void givenValidAuthentication_whenDoFilter_thenProceedsWithFilterChain () throws IOException , ServletException {
49
+ try (MockedStatic <AuthenticationService > mockedAuthService = Mockito .mockStatic (AuthenticationService .class )) {
50
+ mockedAuthService .when (() -> AuthenticationService .getAuthentication (request )).thenReturn (authentication );
51
+
52
+ authenticationFilter .doFilter (request , response , filterChain );
53
+
54
+ mockedAuthService .verify (() -> AuthenticationService .getAuthentication (request ));
55
+ verify (filterChain ).doFilter (request , response );
56
+ verify (response , never ()).setStatus (HttpServletResponse .SC_UNAUTHORIZED );
57
+ }
58
+ }
59
+
60
+ @ Test
61
+ public void givenAuthenticationFailure_whenDoFilter_thenReturnsUnauthorizedResponse () throws IOException , ServletException {
62
+ try (MockedStatic <AuthenticationService > mockedAuthService = Mockito .mockStatic (AuthenticationService .class )) {
63
+ mockedAuthService .when (() -> AuthenticationService .getAuthentication (request ))
64
+ .thenThrow (new RuntimeException ("Authentication failed" ));
65
+
66
+ PrintWriter writer = mock (PrintWriter .class );
67
+ when (response .getWriter ()).thenReturn (writer );
68
+
69
+ authenticationFilter .doFilter (request , response , filterChain );
70
+
71
+ verify (response ).setStatus (HttpServletResponse .SC_UNAUTHORIZED );
72
+ verify (response ).setContentType (MediaType .APPLICATION_JSON_VALUE );
73
+ verify (writer ).print ("Authentication failed" );
74
+ verify (writer ).flush ();
75
+ verify (writer ).close ();
76
+ verify (filterChain , never ()).doFilter (request , response );
77
+ }
78
+ }
79
+ }
0 commit comments