@@ -51,16 +51,14 @@ public void setUp() {
51
51
52
52
@ Test
53
53
public void testLoadUserByUsername_Success () {
54
- // Mock the repository to return the test user
54
+ // Arrange
55
55
when (userRepository .findOneWithAuthoritiesByLogin (anyString ())).thenReturn (Optional .of (testUser ));
56
56
57
- // Call the method under test
57
+ // Act
58
58
UserDetails userDetails = userDetailsService .loadUserByUsername ("testuser" );
59
59
60
- // Verify the interactions with the mock
60
+ // Assert
61
61
verify (userRepository , times (1 )).findOneWithAuthoritiesByLogin ("testuser" );
62
-
63
- // Assert the returned user details
64
62
assertNotNull (userDetails );
65
63
assertEquals ("testuser" , userDetails .getUsername ());
66
64
assertEquals ("encodedPassword" , userDetails .getPassword ());
@@ -74,33 +72,94 @@ public void testLoadUserByUsername_Success() {
74
72
75
73
@ Test
76
74
public void testLoadUserByUsername_UserNotActivated () {
77
- // Create a test user that is not activated
75
+ // Arrange
78
76
User inactiveUser = new User ();
79
77
inactiveUser .setId (1L );
80
78
inactiveUser .setLogin ("inactiveuser" );
81
79
inactiveUser .setPassword ("encodedPassword" );
82
80
inactiveUser .setActivated (false );
83
81
inactiveUser .
setEmail (
"[email protected] " );
84
82
85
- // Mock the repository to return the inactive user
86
83
when (userRepository .findOneWithAuthoritiesByLogin (anyString ())).thenReturn (Optional .of (inactiveUser ));
87
84
88
- // Call the method under test and expect an exception
85
+ // Act & Assert
89
86
assertThrows (UserNotActivatedException .class , () -> userDetailsService .loadUserByUsername ("inactiveuser" ));
90
-
91
- // Verify the interactions with the mock
92
87
verify (userRepository , times (1 )).findOneWithAuthoritiesByLogin ("inactiveuser" );
93
88
}
94
89
95
90
@ Test
96
91
public void testLoadUserByUsername_UserNotFound () {
97
- // Mock the repository to return an empty optional
92
+ // Arrange
98
93
when (userRepository .findOneWithAuthoritiesByLogin (anyString ())).thenReturn (Optional .empty ());
99
94
100
- // Call the method under test and expect an exception
95
+ // Act & Assert
101
96
assertThrows (UsernameNotFoundException .class , () -> userDetailsService .loadUserByUsername ("nonexistentuser" ));
102
-
103
- // Verify the interactions with the mock
104
97
verify (userRepository , times (1 )).findOneWithAuthoritiesByLogin ("nonexistentuser" );
105
98
}
99
+
100
+ @ Test
101
+ public void testLoadUserByUsername_EmptyLogin () {
102
+ // Arrange
103
+ when (userRepository .findOneWithAuthoritiesByLogin (anyString ())).thenReturn (Optional .empty ());
104
+
105
+ // Act & Assert
106
+ assertThrows (UsernameNotFoundException .class , () -> userDetailsService .loadUserByUsername ("" ));
107
+ verify (userRepository , times (1 )).findOneWithAuthoritiesByLogin ("" );
108
+ }
109
+
110
+ @ Test
111
+ public void testLoadUserByUsername_NullLogin () {
112
+
113
+ // Act & Assert
114
+ assertThrows (UsernameNotFoundException .class , () -> userDetailsService .loadUserByUsername (null ));
115
+ }
116
+
117
+ @ Test
118
+ public void testLoadUserByUsername_NoAuthorities () {
119
+ // Arrange
120
+ User userWithoutAuthorities = new User ();
121
+ userWithoutAuthorities .setId (1L );
122
+ userWithoutAuthorities .setLogin ("userwithoutauthorities" );
123
+ userWithoutAuthorities .setPassword ("encodedPassword" );
124
+ userWithoutAuthorities .setActivated (true );
125
+ userWithoutAuthorities .
setEmail (
"[email protected] " );
126
+ userWithoutAuthorities .setAuthorities (Collections .emptySet ());
127
+
128
+ when (userRepository .findOneWithAuthoritiesByLogin (anyString ())).thenReturn (Optional .of (userWithoutAuthorities ));
129
+
130
+ // Act
131
+ UserDetails userDetails = userDetailsService .loadUserByUsername ("userwithoutauthorities" );
132
+
133
+ // Assert
134
+ verify (userRepository , times (1 )).findOneWithAuthoritiesByLogin ("userwithoutauthorities" );
135
+ assertNotNull (userDetails );
136
+ assertEquals ("userwithoutauthorities" , userDetails .getUsername ());
137
+ assertEquals ("encodedPassword" , userDetails .getPassword ());
138
+ assertTrue (userDetails .isAccountNonExpired ());
139
+ assertTrue (userDetails .isAccountNonLocked ());
140
+ assertTrue (userDetails .isCredentialsNonExpired ());
141
+ assertTrue (userDetails .isEnabled ());
142
+ assertTrue (userDetails .getAuthorities ().isEmpty ());
143
+ }
144
+
145
+ @ Test
146
+ public void testLoadUserByUsername_CaseInsensitiveLogin () {
147
+ // Arrange
148
+ when (userRepository .findOneWithAuthoritiesByLogin (anyString ())).thenReturn (Optional .of (testUser ));
149
+
150
+ // Act
151
+ UserDetails userDetails = userDetailsService .loadUserByUsername ("TESTUSER" );
152
+
153
+ // Assert
154
+ verify (userRepository , times (1 )).findOneWithAuthoritiesByLogin ("testuser" );
155
+ assertNotNull (userDetails );
156
+ assertEquals ("testuser" , userDetails .getUsername ());
157
+ assertEquals ("encodedPassword" , userDetails .getPassword ());
158
+ assertTrue (userDetails .isAccountNonExpired ());
159
+ assertTrue (userDetails .isAccountNonLocked ());
160
+ assertTrue (userDetails .isCredentialsNonExpired ());
161
+ assertTrue (userDetails .isEnabled ());
162
+ assertEquals (1 , userDetails .getAuthorities ().size ());
163
+ assertTrue (userDetails .getAuthorities ().contains (new SimpleGrantedAuthority ("ROLE_USER" )));
164
+ }
106
165
}
0 commit comments