diff --git a/src/test/java/com/example/attendance/RollCallServiceImplTest.java b/src/test/java/com/example/attendance/RollCallServiceImplTest.java new file mode 100644 index 0000000..304a8e8 --- /dev/null +++ b/src/test/java/com/example/attendance/RollCallServiceImplTest.java @@ -0,0 +1,117 @@ +package com.example.attendance; + +import com.example.attendance.entity.RollCallResponse; +import com.example.attendance.entity.RollCallSettings; +import com.example.attendance.entity.Student; +import com.example.attendance.service.impl.RollCallServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.MockitoAnnotations; + +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class RollCallServiceImplTest { + + @InjectMocks + private RollCallServiceImpl rollCallService; + + private List students; + private RollCallSettings settings; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + + // 模拟学生列表 + students = Arrays.asList( + createStudent("1", "Alice", 50), + createStudent("2", "Bob", 90), + createStudent("3", "Charlie", 30) + ); + + // 初始化点名设置 + settings = new RollCallSettings(); + settings.setRollCall(true); + settings.setTriggerRandomEvent(false); + settings.setWheelOfFortune(false); + } + + // 测试正常点名模式 + @Test + void testStartRollCall_NormalMode() { + RollCallResponse response = rollCallService.startRollCall(students, settings); + + // 控制台输出 + System.out.println("Test: Normal Mode"); + System.out.println("Student ID: " + response.getStudentId()); + System.out.println("Message: " + response.getMessage()); + + assertNotNull(response); + assertNotNull(response.getStudentId()); + assertTrue(response.getMessage().contains("没有触发随机事件")); + } + + // 测试提问模式 + @Test + void testStartRollCall_QuestionMode() { + settings.setRollCall(false); // 设置为提问模式 + RollCallResponse response = rollCallService.startRollCall(students, settings); + + // 控制台输出 + System.out.println("Test: Question Mode"); + System.out.println("Student ID: " + response.getStudentId()); + System.out.println("Message: " + response.getMessage()); + + assertNotNull(response); + assertNotNull(response.getStudentId()); + assertTrue(response.getMessage().contains("没有触发随机事件")); + } + + // 测试命运轮盘模式 + @Test + void testStartRollCall_WheelOfFortune() { + settings.setWheelOfFortune(true); // 开启命运轮盘 + + RollCallResponse response = rollCallService.startRollCall(students, settings); + + // 控制台输出 + System.out.println("Test: Wheel of Fortune Mode"); + System.out.println("Student ID: " + response.getStudentId()); + System.out.println("Message: " + response.getMessage()); + + assertNotNull(response); + assertNotNull(response.getStudentId()); + assertTrue(response.getMessage().contains("命运轮盘")); + } + + // 测试触发随机事件 + @Test + void testStartRollCall_TriggerRandomEvent() { + settings.setTriggerRandomEvent(true); // 触发随机事件 + + RollCallResponse response = rollCallService.startRollCall(students, settings); + + // 控制台输出 + System.out.println("Test: Random Event Triggered"); + System.out.println("Student ID: " + response.getStudentId()); + System.out.println("Message: " + response.getMessage()); + + assertNotNull(response); + assertNotNull(response.getStudentId()); + assertTrue(response.getMessage().contains("触发了随机事件")); + } + + // 辅助方法,创建学生 + private Student createStudent(String id, String name, int points) { + Student student = new Student(); + student.setStudentNumber(id); + student.setName(name); + student.setPoints(BigDecimal.valueOf(points)); + return student; + } +} diff --git a/src/test/java/com/example/attendance/StudentServiceImplTest.java b/src/test/java/com/example/attendance/StudentServiceImplTest.java new file mode 100644 index 0000000..1747ba3 --- /dev/null +++ b/src/test/java/com/example/attendance/StudentServiceImplTest.java @@ -0,0 +1,162 @@ +package com.example.attendance; + +import com.example.attendance.entity.Student; +import com.example.attendance.mapper.StudentMapper; +import com.example.attendance.service.impl.StudentServiceImpl; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.web.multipart.MultipartFile; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class StudentServiceImplTest { + + @InjectMocks + private StudentServiceImpl studentService; + + @Mock + private StudentMapper studentMapper; + + @Mock + private MultipartFile file; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testImportStudents() throws Exception { + // 模拟 Excel 文件内容 + XSSFWorkbook workbook = new XSSFWorkbook(); + var sheet = workbook.createSheet("Sheet 1"); + var header = sheet.createRow(0); + header.createCell(0).setCellValue("Student Number"); + header.createCell(1).setCellValue("Name"); + header.createCell(2).setCellValue("Points"); + + var row1 = sheet.createRow(1); + row1.createCell(0).setCellValue("1"); + row1.createCell(1).setCellValue("Alice"); + row1.createCell(2).setCellValue(50); + + var row2 = sheet.createRow(2); + row2.createCell(0).setCellValue("2"); + row2.createCell(1).setCellValue("Bob"); + row2.createCell(2).setCellValue(80); + + // 将 workbook 写入 ByteArrayOutputStream + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + workbook.write(outputStream); + workbook.close(); // 关闭 workbook + + // 将 ByteArrayOutputStream 转换为 InputStream + InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); + when(file.getInputStream()).thenReturn(inputStream); + + // 调用 importStudents 方法 + studentService.importStudents(file); + + // 验证是否保存了学生数据 + verify(studentMapper, times(1)).saveStudents(anyList()); + } + + // 测试调整积分 + @Test + void testAdjustPoints() { + // 模拟学生数据 + Student student = new Student(); + student.setStudentNumber("1"); + student.setPoints(BigDecimal.valueOf(50)); + + when(studentMapper.findByStudentNumber("1")).thenReturn(student); + + // 调用 adjustPoints 方法 + studentService.adjustPoints("1", BigDecimal.valueOf(10)); + + // 验证是否更新了学生数据 + assertEquals(BigDecimal.valueOf(60), student.getPoints()); + verify(studentMapper, times(1)).update(student); + } + + // 测试获取学生排行榜 + @Test + void testGetStudentRanking() { + List mockStudents = new ArrayList<>(); + Student student1 = new Student(); + student1.setName("Alice"); + student1.setPoints(BigDecimal.valueOf(100)); + + Student student2 = new Student(); + student2.setName("Bob"); + student2.setPoints(BigDecimal.valueOf(90)); + + mockStudents.add(student1); + mockStudents.add(student2); + + when(studentMapper.findStudentsByRanking(0, 2)).thenReturn(mockStudents); + + // 调用 getStudentRanking 方法 + List ranking = studentService.getStudentRanking(0, 2); + + // 验证返回结果是否正确 + assertEquals(2, ranking.size()); + assertEquals("Alice", ranking.get(0).getName()); + assertEquals("Bob", ranking.get(1).getName()); + } + + // 测试根据学生编号查找 + @Test + void testFindByStudentNumber() { + Student student = new Student(); + student.setStudentNumber("1"); + student.setName("Alice"); + + when(studentMapper.findByStudentNumber("1")).thenReturn(student); + + // 调用 findByStudentNumber 方法 + Student result = studentService.findByStudentNumber("1"); + + // 验证结果 + assertNotNull(result); + assertEquals("Alice", result.getName()); + } + + // 测试保存学生 + @Test + void testSave() { + Student student = new Student(); + student.setName("Alice"); + + // 调用 save 方法 + studentService.save(student); + + // 验证是否保存了学生 + verify(studentMapper, times(1)).save(student); + } + + // 测试删除学生 + @Test + void testDelete() { + Long id = 1L; + + // 调用 delete 方法 + studentService.delete(id); + + // 验证是否删除了学生 + verify(studentMapper, times(1)).delete(id); + } + +} diff --git a/src/test/java/com/example/attendance/TeacherServiceImplTest.java b/src/test/java/com/example/attendance/TeacherServiceImplTest.java new file mode 100644 index 0000000..6880448 --- /dev/null +++ b/src/test/java/com/example/attendance/TeacherServiceImplTest.java @@ -0,0 +1,120 @@ +package com.example.attendance; + +import com.example.attendance.entity.Teacher; +import com.example.attendance.mapper.TeacherMapper; +import com.example.attendance.service.impl.TeacherServiceImpl; +import com.example.attendance.util.JWTUtil; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.security.crypto.password.PasswordEncoder; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +class TeacherServiceImplTest { + + @InjectMocks + private TeacherServiceImpl teacherService; // 使用 @InjectMocks 注入测试的服务类 + + @Mock + private TeacherMapper teacherMapper; // 模拟 TeacherMapper + + @Mock + private PasswordEncoder passwordEncoder; // 模拟 PasswordEncoder + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); // 初始化 mocks + } + + // 测试注册功能 + @Test + void testRegister() { + // 模拟加密的密码 + String rawPassword = "password123"; + String encodedPassword = "encodedPassword123"; + + when(passwordEncoder.encode(rawPassword)).thenReturn(encodedPassword); + + // 调用注册方法 + teacherService.register("testUser", rawPassword); + + // 验证 teacherMapper.save 是否被调用,并且密码是加密的 + verify(teacherMapper, times(1)).save(any(Teacher.class)); + verify(passwordEncoder, times(1)).encode(rawPassword); + } + + // 测试登录功能 + @Test + void testLogin_Success() { + // 模拟数据库中保存的教师 + Teacher teacher = new Teacher(); + teacher.setUsername("testUser"); + teacher.setPassword("encodedPassword123"); + + // 模拟找到用户,并且密码匹配 + when(teacherMapper.findByUsername("testUser")).thenReturn(teacher); + when(passwordEncoder.matches("password123", "encodedPassword123")).thenReturn(true); + + // 模拟生成的 JWT token + String token = "mockedJWTToken"; + mockStatic(JWTUtil.class); // mock 静态方法 + when(JWTUtil.generateToken(teacher)).thenReturn(token); + + // 调用登录方法 + String resultToken = teacherService.login("testUser", "password123"); + + // 验证生成的 token 是否正确 + assertEquals(token, resultToken); + + // 验证方法调用 + verify(teacherMapper, times(1)).findByUsername("testUser"); + verify(passwordEncoder, times(1)).matches("password123", "encodedPassword123"); + } + + // 测试登录失败(用户名不正确) + @Test + void testLogin_Fail_UserNotFound() { + // 模拟未找到用户 + when(teacherMapper.findByUsername("nonExistentUser")).thenReturn(null); + + // 调用登录方法并捕获异常 + Exception exception = assertThrows(RuntimeException.class, () -> { + teacherService.login("nonExistentUser", "password123"); + }); + + // 验证异常消息 + assertEquals("用户名或密码不正确", exception.getMessage()); + + // 验证 teacherMapper.findByUsername 被调用一次 + verify(teacherMapper, times(1)).findByUsername("nonExistentUser"); + } + + // 测试登录失败(密码不匹配) + @Test + void testLogin_Fail_WrongPassword() { + // 模拟找到用户,但密码不匹配 + Teacher teacher = new Teacher(); + teacher.setUsername("testUser"); + teacher.setPassword("encodedPassword123"); + + when(teacherMapper.findByUsername("testUser")).thenReturn(teacher); + when(passwordEncoder.matches("wrongPassword", "encodedPassword123")).thenReturn(false); + + // 调用登录方法并捕获异常 + Exception exception = assertThrows(RuntimeException.class, () -> { + teacherService.login("testUser", "wrongPassword"); + }); + + // 验证异常消息 + assertEquals("用户名或密码不正确", exception.getMessage()); + + // 验证方法调用 + verify(teacherMapper, times(1)).findByUsername("testUser"); + verify(passwordEncoder, times(1)).matches("wrongPassword", "encodedPassword123"); + } +}