Skip to content

Commit 503d653

Browse files
MrJovanovic13jzheaux
authored andcommitted
Add InMemoryUserDetailsManager tests
Tests added: createUserWhenUserAlreadyExistsThenException updateUserWhenUserDoesNotExistThenException loadUserByUsernameWhenUserNullThenException Issue gh-3192
1 parent 435b467 commit 503d653

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

core/src/test/java/org/springframework/security/provisioning/InMemoryUserDetailsManagerTests.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,8 +27,10 @@
2727
import org.springframework.security.core.userdetails.PasswordEncodedUser;
2828
import org.springframework.security.core.userdetails.User;
2929
import org.springframework.security.core.userdetails.UserDetails;
30+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
3031

3132
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3234
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3335
import static org.mockito.BDDMockito.given;
3436
import static org.mockito.Mockito.mock;
@@ -97,4 +99,24 @@ public void changePasswordWhenCustomSecurityContextHolderStrategyThenUses() {
9799
verify(strategy).getContext();
98100
}
99101

102+
@Test
103+
public void createUserWhenUserAlreadyExistsThenException() {
104+
assertThatIllegalArgumentException().isThrownBy(() -> this.manager.createUser(this.user))
105+
.withMessage("user should not exist");
106+
}
107+
108+
@Test
109+
public void updateUserWhenUserDoesNotExistThenException() {
110+
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
111+
assertThatIllegalArgumentException().isThrownBy(() -> manager.updateUser(this.user))
112+
.withMessage("user should exist");
113+
}
114+
115+
@Test
116+
public void loadUserByUsernameWhenUserNullThenException() {
117+
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
118+
assertThatExceptionOfType(UsernameNotFoundException.class)
119+
.isThrownBy(() -> manager.loadUserByUsername(this.user.getUsername()));
120+
}
121+
100122
}

0 commit comments

Comments
 (0)