44import static org .junit .jupiter .api .Assertions .assertNotNull ;
55import static org .junit .jupiter .api .Assertions .assertThrows ;
66import static org .mockito .ArgumentMatchers .any ;
7+ import static org .mockito .Mockito .mock ;
8+ import static org .mockito .Mockito .verify ;
9+ import static org .mockito .Mockito .verifyNoInteractions ;
710import static org .mockito .Mockito .when ;
811
912import org .junit .jupiter .api .BeforeEach ;
2124import com .github .renancvitor .inventory .application .authentication .dto .JWTTokenData ;
2225import com .github .renancvitor .inventory .application .authentication .dto .LoginData ;
2326import com .github .renancvitor .inventory .application .authentication .service .AuthenticationService ;
27+ import com .github .renancvitor .inventory .application .user .dto .UserSummaryData ;
28+
29+ import jakarta .servlet .http .Cookie ;
30+ import jakarta .servlet .http .HttpServletResponse ;
2431
2532@ ExtendWith (MockitoExtension .class )
2633@ ActiveProfiles ("test" )
@@ -48,25 +55,45 @@ void setup() {
4855 class PositiveCases {
4956 @ Test
5057 void shouldReturn200AndJWTTokenDataWhenAuthenticationSucceeds () {
51- when (authenticationService .authentication (any (LoginData .class ), any (AuthenticationManager .class )))
58+ HttpServletResponse response = mock (HttpServletResponse .class );
59+
60+ when (authenticationService .authentication (
61+ any (LoginData .class ),
62+ any (AuthenticationManager .class )))
5263 .thenReturn (jwtTokenData );
5364
54- ResponseEntity <JWTTokenData > response = authenticationController .authentication (loginData );
65+ ResponseEntity <UserSummaryData > result = authenticationController .authentication (loginData , response );
66+
67+ assertNotNull (result );
68+ assertEquals (200 , result .getStatusCode ().value ());
69+ assertEquals (jwtTokenData .user (), result .getBody ());
70+
71+ verify (authenticationService ).authentication (
72+ any (LoginData .class ),
73+ any (AuthenticationManager .class ));
5574
56- assertNotNull (response );
57- assertEquals (200 , response .getStatusCode ().value ());
58- assertEquals (jwtTokenData , response .getBody ());
75+ verify (response ).addCookie (any (Cookie .class ));
5976 }
6077 }
6178
6279 @ Nested
6380 class NegativeCases {
6481 @ Test
6582 void shouldPropagateExceptionWhenServiceThrows () {
66- when (authenticationService .authentication (any (LoginData .class ), any (AuthenticationManager .class )))
83+ HttpServletResponse response = mock (HttpServletResponse .class );
84+
85+ when (authenticationService .authentication (
86+ any (LoginData .class ),
87+ any (AuthenticationManager .class )))
6788 .thenThrow (new RuntimeException ("Auth failed" ));
6889
69- assertThrows (RuntimeException .class , () -> authenticationController .authentication (loginData ));
90+ assertThrows (RuntimeException .class , () -> authenticationController .authentication (loginData , response ));
91+
92+ verify (authenticationService ).authentication (
93+ any (LoginData .class ),
94+ any (AuthenticationManager .class ));
95+
96+ verifyNoInteractions (response );
7097 }
7198 }
7299}
0 commit comments