test(auth): 添加单元测试

- PasswordServiceTest: 5个测试
- UserServiceTest: 8个测试
- 添加测试依赖: spring-boot-starter-test, h2
This commit is contained in:
chiguyong 2026-03-17 23:30:41 +08:00
parent f022d298e6
commit d12edef2ce
4 changed files with 208 additions and 0 deletions

View File

@ -85,5 +85,17 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,46 @@
package com.ether.pms.auth.service;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(MockitoExtension.class)
class PasswordServiceTest {
@InjectMocks
private PasswordService passwordService;
@Test
void encode_shouldReturnNonNullValue() {
String encoded = passwordService.encode("Test123!");
assertNotNull(encoded);
assertNotEquals("Test123!", encoded);
}
@Test
void matches_shouldReturnTrue_forCorrectPassword() {
String encoded = passwordService.encode("Test123!");
assertTrue(passwordService.matches("Test123!", encoded));
}
@Test
void matches_shouldReturnFalse_forWrongPassword() {
String encoded = passwordService.encode("Test123!");
assertFalse(passwordService.matches("Wrong123!", encoded));
}
@Test
void isPasswordWeak_shouldReturnTrue_forCommonWeakPasswords() {
assertTrue(passwordService.isPasswordWeak("password123"));
assertTrue(passwordService.isPasswordWeak("admin123"));
assertTrue(passwordService.isPasswordWeak("qwerty123"));
}
@Test
void isPasswordWeak_shouldReturnFalse_forStrongPasswords() {
assertFalse(passwordService.isPasswordWeak("Str0ng!Pass"));
}
}

View File

@ -0,0 +1,136 @@
package com.ether.pms.auth.service;
import com.ether.pms.auth.entity.User;
import com.ether.pms.auth.repository.RoleRepository;
import com.ether.pms.auth.repository.UserRepository;
import com.ether.pms.common.BusinessException;
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 java.util.Optional;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
@Mock
private UserRepository userRepository;
@Mock
private RoleRepository roleRepository;
@Mock
private PasswordService passwordService;
@InjectMocks
private UserService userService;
@Test
void findById_shouldReturnUser_whenExists() {
UUID userId = UUID.randomUUID();
User mockUser = new User();
mockUser.setId(userId);
mockUser.setUsername("testuser");
when(userRepository.findById(userId)).thenReturn(Optional.of(mockUser));
User result = userService.findById(userId);
assertNotNull(result);
assertEquals("testuser", result.getUsername());
}
@Test
void findById_shouldThrowException_whenNotExists() {
UUID userId = UUID.randomUUID();
when(userRepository.findById(userId)).thenReturn(Optional.empty());
assertThrows(BusinessException.class, () -> userService.findById(userId));
}
@Test
void findByUsername_shouldReturnUser_whenExists() {
User mockUser = new User();
mockUser.setUsername("testuser");
when(userRepository.findByUsername("testuser")).thenReturn(Optional.of(mockUser));
User result = userService.findByUsername("testuser");
assertNotNull(result);
assertEquals("testuser", result.getUsername());
}
@Test
void create_shouldThrowException_whenUsernameExists() {
User newUser = new User();
newUser.setUsername("existinguser");
newUser.setPassword("Test123!");
when(userRepository.existsByUsername("existinguser")).thenReturn(true);
assertThrows(BusinessException.class, () -> userService.create(newUser));
}
@Test
void create_shouldThrowException_whenPasswordTooWeak() {
User newUser = new User();
newUser.setUsername("newuser");
newUser.setPassword("weak");
when(userRepository.existsByUsername("newuser")).thenReturn(false);
doThrow(new IllegalArgumentException("密码太弱"))
.when(passwordService).validatePassword("weak");
assertThrows(BusinessException.class, () -> userService.create(newUser));
}
@Test
void create_shouldEncodePassword_whenValid() {
User newUser = new User();
newUser.setUsername("newuser");
newUser.setPassword("Valid123!");
when(userRepository.existsByUsername("newuser")).thenReturn(false);
doNothing().when(passwordService).validatePassword("Valid123!");
when(passwordService.isPasswordWeak("Valid123!")).thenReturn(false);
when(passwordService.encode("Valid123!")).thenReturn("encodedPassword");
when(userRepository.save(any(User.class))).thenReturn(newUser);
User result = userService.create(newUser);
assertNotNull(result);
verify(passwordService).encode("Valid123!");
}
@Test
void updatePassword_shouldThrowException_whenOldPasswordWrong() {
UUID userId = UUID.randomUUID();
User existingUser = new User();
existingUser.setId(userId);
existingUser.setPassword("encodedOld");
when(userRepository.findById(userId)).thenReturn(Optional.of(existingUser));
when(passwordService.matches("wrongOld", "encodedOld")).thenReturn(false);
assertThrows(BusinessException.class,
() -> userService.updatePassword(userId, "wrongOld", "New123!"));
}
@Test
void delete_shouldCallRepository() {
UUID userId = UUID.randomUUID();
doNothing().when(userRepository).deleteById(userId);
userService.delete(userId);
verify(userRepository).deleteById(userId);
}
}

14
pom.xml
View File

@ -101,6 +101,20 @@
<artifactId>redisson-spring-boot-starter</artifactId> <artifactId>redisson-spring-boot-starter</artifactId>
<version>${redisson.version}</version> <version>${redisson.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>