test追加
This commit is contained in:
168
src/test/java/co/jp/app/UserServiceTest.java
Normal file
168
src/test/java/co/jp/app/UserServiceTest.java
Normal file
@ -0,0 +1,168 @@
|
|||||||
|
package co.jp.app;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.junit.jupiter.api.function.Executable;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
|
import co.jp.app.common.ResultCode;
|
||||||
|
import co.jp.app.dto.RegistrationDto;
|
||||||
|
import co.jp.app.entity.UserEntity;
|
||||||
|
import co.jp.app.exception.BusinessException;
|
||||||
|
import co.jp.app.repository.UserRepository;
|
||||||
|
import co.jp.app.service.UserService;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class UserServiceTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
private RegistrationDto registrationDto;
|
||||||
|
private UserEntity userEntity;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
registrationDto = new RegistrationDto();
|
||||||
|
registrationDto.setName("Test User");
|
||||||
|
registrationDto.setEmail("test@example.com");
|
||||||
|
registrationDto.setPassword("password123");
|
||||||
|
|
||||||
|
userEntity = new UserEntity();
|
||||||
|
userEntity.setName("Test User");
|
||||||
|
userEntity.setEmail("test@example.com");
|
||||||
|
userEntity.setPassword("encodedPassword");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("新用户注册成功")
|
||||||
|
public void registerNewUser_success() throws BusinessException {
|
||||||
|
// Arrange
|
||||||
|
when(userRepository.existsByEmail(anyString())).thenReturn(false);
|
||||||
|
when(passwordEncoder.encode(anyString())).thenReturn("encodedPassword");
|
||||||
|
when(userRepository.save(any(UserEntity.class))).thenReturn(userEntity);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
UserEntity savedUser = userService.registerNewUser(registrationDto);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertNotNull(savedUser);
|
||||||
|
assertEquals(userEntity.getEmail(), savedUser.getEmail());
|
||||||
|
assertEquals("encodedPassword", savedUser.getPassword());
|
||||||
|
verify(userRepository, times(1)).existsByEmail("test@example.com");
|
||||||
|
verify(passwordEncoder, times(1)).encode("password123");
|
||||||
|
verify(userRepository, times(1)).save(any(UserEntity.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("用户注册 - 邮箱已存在")
|
||||||
|
public void registerNewUser_emailAlreadyExists() {
|
||||||
|
// Arrange
|
||||||
|
when(userRepository.existsByEmail(anyString())).thenReturn(true);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
BusinessException exception = assertThrows(BusinessException.class, new Executable() {
|
||||||
|
public void execute() throws Throwable {
|
||||||
|
userService.registerNewUser(registrationDto);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(ResultCode.USER_EMAIL_ALREADY_EXISTS, exception.getResultCode());
|
||||||
|
verify(userRepository, times(1)).existsByEmail("test@example.com");
|
||||||
|
verify(passwordEncoder, never()).encode(anyString());
|
||||||
|
verify(userRepository, never()).save(any(UserEntity.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("用户注册 - 密码过短")
|
||||||
|
public void registerNewUser_passwordTooShort() {
|
||||||
|
// Arrange
|
||||||
|
registrationDto.setPassword("123"); // 设置一个短密码
|
||||||
|
when(userRepository.existsByEmail(anyString())).thenReturn(false);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
BusinessException exception = assertThrows(BusinessException.class, new Executable() {
|
||||||
|
public void execute() throws Throwable {
|
||||||
|
userService.registerNewUser(registrationDto);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(ResultCode.USER_PASSWORD_TOO_SHORT, exception.getResultCode());
|
||||||
|
verify(userRepository, times(1)).existsByEmail("test@example.com");
|
||||||
|
verify(passwordEncoder, never()).encode(anyString());
|
||||||
|
verify(userRepository, never()).save(any(UserEntity.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("用户注册 - 密码为null")
|
||||||
|
public void registerNewUser_passwordIsNull() {
|
||||||
|
// Arrange
|
||||||
|
registrationDto.setPassword(null); // 设置密码为null
|
||||||
|
when(userRepository.existsByEmail(anyString())).thenReturn(false);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
BusinessException exception = assertThrows(BusinessException.class, new Executable() {
|
||||||
|
public void execute() throws Throwable {
|
||||||
|
userService.registerNewUser(registrationDto);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(ResultCode.USER_PASSWORD_TOO_SHORT, exception.getResultCode());
|
||||||
|
verify(userRepository, times(1)).existsByEmail("test@example.com");
|
||||||
|
verify(passwordEncoder, never()).encode(anyString());
|
||||||
|
verify(userRepository, never()).save(any(UserEntity.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("通过邮箱加载用户 - 用户存在")
|
||||||
|
public void loadUserByUsername_userFound() {
|
||||||
|
// Arrange
|
||||||
|
when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(userEntity));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
UserDetails userDetails = userService.loadUserByUsername("test@example.com");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertNotNull(userDetails);
|
||||||
|
assertEquals(userEntity.getEmail(), userDetails.getUsername());
|
||||||
|
assertEquals(userEntity.getPassword(), userDetails.getPassword());
|
||||||
|
assertTrue(userDetails.getAuthorities().stream()
|
||||||
|
.anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("ROLE_USER")));
|
||||||
|
verify(userRepository, times(1)).findByEmail("test@example.com");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("通过邮箱加载用户 - 用户不存在")
|
||||||
|
public void loadUserByUsername_userNotFound() {
|
||||||
|
// Arrange
|
||||||
|
when(userRepository.findByEmail(anyString())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
UsernameNotFoundException exception = assertThrows(UsernameNotFoundException.class, new Executable() {
|
||||||
|
public void execute() throws Throwable {
|
||||||
|
userService.loadUserByUsername("unknown@example.com");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals("unknown@example.com not found", exception.getMessage());
|
||||||
|
verify(userRepository, times(1)).findByEmail("unknown@example.com");
|
||||||
|
}
|
||||||
|
}
|
70
src/test/java/co/jp/app/UserServiceTest1.java
Normal file
70
src/test/java/co/jp/app/UserServiceTest1.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package co.jp.app;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
|
import co.jp.app.dto.RegistrationDto;
|
||||||
|
import co.jp.app.entity.UserEntity;
|
||||||
|
import co.jp.app.exception.BusinessException;
|
||||||
|
import co.jp.app.repository.UserRepository;
|
||||||
|
import co.jp.app.service.UserService;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class UserServiceTest1 {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
private RegistrationDto registrationDto;
|
||||||
|
private UserEntity userEntity;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
registrationDto = new RegistrationDto();
|
||||||
|
registrationDto.setName("Test User");
|
||||||
|
registrationDto.setEmail("test@example.com");
|
||||||
|
registrationDto.setPassword("password123");
|
||||||
|
|
||||||
|
userEntity = new UserEntity();
|
||||||
|
userEntity.setName("Test User");
|
||||||
|
userEntity.setEmail("test@example.com");
|
||||||
|
userEntity.setPassword("encodedPassword");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("新用户注册成功")
|
||||||
|
public void registerNewUser_success() throws BusinessException {
|
||||||
|
// Arrange
|
||||||
|
when(userRepository.existsByEmail(anyString())).thenReturn(false);
|
||||||
|
when(passwordEncoder.encode(anyString())).thenReturn("encodedPassword");
|
||||||
|
when(userRepository.save(any(UserEntity.class))).thenReturn(userEntity);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
UserEntity savedUser = userService.registerNewUser(registrationDto);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertNotNull(savedUser);
|
||||||
|
assertEquals(userEntity.getEmail(), savedUser.getEmail());
|
||||||
|
assertEquals("encodedPassword", savedUser.getPassword());
|
||||||
|
verify(userRepository, times(1)).existsByEmail("test@example.com");
|
||||||
|
verify(passwordEncoder, times(1)).encode("password123");
|
||||||
|
verify(userRepository, times(1)).save(any(UserEntity.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
BIN
target/test-classes/co/jp/app/UserServiceTest$1.class
Normal file
BIN
target/test-classes/co/jp/app/UserServiceTest$1.class
Normal file
Binary file not shown.
BIN
target/test-classes/co/jp/app/UserServiceTest$2.class
Normal file
BIN
target/test-classes/co/jp/app/UserServiceTest$2.class
Normal file
Binary file not shown.
BIN
target/test-classes/co/jp/app/UserServiceTest$3.class
Normal file
BIN
target/test-classes/co/jp/app/UserServiceTest$3.class
Normal file
Binary file not shown.
BIN
target/test-classes/co/jp/app/UserServiceTest$4.class
Normal file
BIN
target/test-classes/co/jp/app/UserServiceTest$4.class
Normal file
Binary file not shown.
BIN
target/test-classes/co/jp/app/UserServiceTest.class
Normal file
BIN
target/test-classes/co/jp/app/UserServiceTest.class
Normal file
Binary file not shown.
BIN
target/test-classes/co/jp/app/UserServiceTest1.class
Normal file
BIN
target/test-classes/co/jp/app/UserServiceTest1.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user