Users Service层测试

This commit is contained in:
myh 2023-11-15 17:34:27 +08:00
parent 3c3f2a8cc5
commit 337a77f5e7

View File

@ -0,0 +1,131 @@
package com.example.springdemo.service;
import com.example.springdemo.dao.UsersPasswordRepository;
import com.example.springdemo.dao.UsersRepository;
import com.example.springdemo.entities.Users;
import com.example.springdemo.entities.password.UsersPassword;
import jakarta.annotation.Resource;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.annotation.Rollback;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class UsersServiceTests {
@Mock
private UsersRepository usersRepository;
@Mock
private UsersPasswordRepository usersPasswordRepository;
@Resource
@InjectMocks
private UsersServiceImpl usersService;
private Users user1;
private UsersPassword user1Password;
private Users user2;
private UsersPassword user2Password;
List<Users> usersList;
List<UsersPassword> usersPasswordList;
@Data
@NotNull
public static class wrapperUserAndPassword {
public Users user;
public UsersPassword userPassword;
}
@BeforeEach
public void setUp() {
usersList = new ArrayList<>();
usersPasswordList = new ArrayList<>();
user1 = new Users();
user1.setName("user1");
user1.setSex("");
user1.setPhoneNumber("13988887777");
user1.setAddress("test address user1");
user1Password = new UsersPassword();
user1Password.setUsers(user1);
user1Password.setPassword("TestPassword1");
user2 = new Users();
user2.setName("user2");
user2.setSex("");
user2.setPhoneNumber("13299994444");
user2.setAddress("test address user2");
user2Password = new UsersPassword();
user2Password.setUsers(user2);
user2Password.setPassword("TestPassword2");
usersList.add(user1);
usersPasswordList.add(user1Password);
usersList.add(user2);
usersPasswordList.add(user2Password);
}
@AfterEach
public void tearDown() {
user1 = null;
user1Password = null;
user2 = null;
user2Password = null;
usersList = null;
usersPasswordList = null;
}
@Test
void contextLoads() {
}
@Test
@Rollback(value = true)
void testSaveUser() {
when(usersRepository.save(any())).thenReturn(user1);
usersService.addUser(user1, user1Password);
verify(usersRepository, times(1)).save(any());
}
@Test
@Rollback(value = true)
void testGetAllUsers() {
usersService.addUser(user1, user1Password);
when(usersRepository.findAll()).thenReturn(usersList);
List<Users> usersList1 = usersService.findAllUsers();
Assertions.assertEquals(usersList1, usersList);
verify(usersRepository, times(1)).save(user1);
verify(usersRepository, times(1)).findAll();
}
@Test
@Rollback(value = true)
void testGetUserById() {
Mockito.when(usersRepository.findById(2L)).thenReturn(java.util.Optional.ofNullable(user1));
assertThat(usersService.findById(2L).orElse(null)).isEqualTo(user1);
}
@Test
@Rollback
void testGetUserByName() {
//TODO: 未测试
Mockito.when(usersRepository.findByName("user1")).thenReturn(java.util.Optional.ofNullable(user1));
assertThat(usersService.findByName("user1").orElse(null)).isEqualTo(user1);
}
}