Add unit tests for setUserDetailsMapper method

Signed-off-by: dae won <eodnjs01477@gmail.com>
This commit is contained in:
dae won 2025-02-15 21:09:29 +09:00 committed by Josh Cummings
parent 22511aac7f
commit cb07031259
2 changed files with 34 additions and 0 deletions

View File

@ -166,6 +166,17 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
setDataSource(dataSource);
}
/**
* Sets the {@code RowMapper} to convert each user result row into a
* {@link UserDetails} object.
*
* The default mapper expects columns with names like 'username', 'password',
* 'enabled', etc., and maps them directly to the corresponding UserDetails
* properties.
* @param mapper the {@code RowMapper} to use for mapping rows in the database, must
* not be null
* @since 6.5
*/
public void setUserDetailsMapper(RowMapper<UserDetails> mapper) {
Assert.notNull(mapper, "userDetailsMapper cannot be null");
this.userDetailsMapper = mapper;

View File

@ -16,6 +16,7 @@
package org.springframework.security.provisioning;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -28,6 +29,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.PopulatedDatabase;
import org.springframework.security.TestDataSource;
import org.springframework.security.access.AccessDeniedException;
@ -48,14 +50,17 @@ import org.springframework.security.core.userdetails.UserDetails;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Tests for {@link JdbcUserDetailsManager}
*
* @author Luke Taylor
* @author dae won
*/
public class JdbcUserDetailsManagerTests {
@ -365,6 +370,24 @@ public class JdbcUserDetailsManagerTests {
assertThat(updatedAuth.getCredentials()).isNull();
}
@Test
public void setUserDetailsMapperWithNullMapperThrowsException() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> this.manager.setUserDetailsMapper(null))
.withMessage("userDetailsMapper cannot be null");
}
@Test
public void setUserDetailsMapperWithMockMapper() throws SQLException {
RowMapper<UserDetails> mockMapper = mock(RowMapper.class);
when(mockMapper.mapRow(any(), anyInt())).thenReturn(joe);
this.manager.setUserDetailsMapper(mockMapper);
insertJoe();
UserDetails newJoe = this.manager.loadUserByUsername("joe");
assertThat(joe).isEqualTo(newJoe);
verify(mockMapper).mapRow(any(), anyInt());
}
private Authentication authenticateJoe() {
UsernamePasswordAuthenticationToken auth = UsernamePasswordAuthenticationToken.authenticated("joe", "password",
joe.getAuthorities());