测试:增加service层单元测试

This commit is contained in:
seahi 2024-12-24 09:37:35 +08:00
parent 8f1c8815d7
commit 778810e0ed
3 changed files with 344 additions and 0 deletions

View File

@ -0,0 +1,186 @@
package test.service;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Calendar;
import dao.LeaveRequestDAO;
import dao.StudentDAO;
import dao.impl.LeaveRequestDAOImpl;
import dao.impl.StudentDAOImpl;
import model.LeaveRequest;
import model.Student;
import model.ApprovalStatus;
import service.LeaveRequestService;
import service.ServiceFactory;
import util.DatabaseUtil;
public class LeaveRequestServiceTest {
private LeaveRequestService leaveRequestService;
private LeaveRequestDAO leaveRequestDAO;
private StudentDAO studentDAO;
private static final String TEST_STUDENT_ID = "S2024001";
@Before
public void setUp() throws SQLException {
leaveRequestService = ServiceFactory.getLeaveRequestService();
leaveRequestDAO = new LeaveRequestDAOImpl();
studentDAO = new StudentDAOImpl();
// 清理测试数据
cleanTestData();
// 准备测试数据
Student student = createTestStudent();
studentDAO.insert(student);
}
@After
public void tearDown() throws SQLException {
cleanTestData();
}
private void cleanTestData() throws SQLException {
try (Connection conn = DatabaseUtil.getConnection()) {
// 清理请假申请数据
Student student = studentDAO.findByStudentId(TEST_STUDENT_ID);
if (student == null) {
student = createTestStudent();
}
try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM leave_requests WHERE student_id = ?")) {
stmt.setInt(1, student.getId());
stmt.executeUpdate();
}
// 清理学生数据
try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM students WHERE id = ?")) {
stmt.setInt(1, student.getId());
stmt.executeUpdate();
}
}
}
private Student createTestStudent() {
Student student = new Student();
student.setStudentId(TEST_STUDENT_ID);
student.setName("测试学生");
student.setClassName("2024级1班");
student.setCollege("计算机学院");
student.setMajor("软件工程");
student.setContact("13900139000");
student.setPassword("password123");
return student;
}
private LeaveRequest createTestLeaveRequest() {
LeaveRequest request = new LeaveRequest();
// 从数据库获取完整的Student对象
Student student = studentDAO.findByStudentId(TEST_STUDENT_ID);
if (student == null) {
student = createTestStudent();
}
request.setStudent(student);
// 设置开始时间为当前时间
Calendar calendar = Calendar.getInstance();
request.setStartTime(calendar.getTime());
// 设置结束时间为3天后
calendar.add(Calendar.DAY_OF_MONTH, 3);
request.setEndTime(calendar.getTime());
request.setDuration(72.0); // 3天 = 72小时
request.setLocation("北京");
request.setReasonType("事假");
request.setReasonDetail("测试请假原因");
request.setLeavingCity(true);
request.setStatus(ApprovalStatus.PENDING);
return request;
}
@Test
public void testSubmitLeaveRequest() {
LeaveRequest request = createTestLeaveRequest();
boolean result = leaveRequestService.submitLeaveRequest(request);
assertTrue("提交请假申请应该成功", result);
// 验证请假申请是否被正确保存
Student student = studentDAO.findByStudentId(TEST_STUDENT_ID);
LeaveRequest savedRequest = leaveRequestDAO.findByStudentId(student.getId()).get(0);
assertNotNull("应该能找到保存的请假申请", savedRequest);
assertEquals("请假原因应该匹配", request.getReasonDetail(), savedRequest.getReasonDetail());
assertEquals("申请状态应该是PENDING", ApprovalStatus.PENDING, savedRequest.getStatus());
}
@Test
public void testGetStudentLeaveRequests() {
// 先提交一个请假申请
LeaveRequest request = createTestLeaveRequest();
boolean submitResult = leaveRequestService.submitLeaveRequest(request);
assertTrue("提交请假申请应该成功", submitResult);
// 获取该学生的所有请假申请
Student student = studentDAO.findByStudentId(TEST_STUDENT_ID);
List<LeaveRequest> requests = leaveRequestService.getStudentLeaveRequests(student.getId());
assertNotNull("返回的请假申请列表不应为null", requests);
assertFalse("请假申请列表不应为空", requests.isEmpty());
assertEquals("应该只有一个请假申请", 1, requests.size());
LeaveRequest savedRequest = requests.get(0);
assertEquals("学生ID应该匹配", student.getId(), savedRequest.getStudent().getId());
assertEquals("请假原因应该匹配", request.getReasonDetail(), savedRequest.getReasonDetail());
}
@Test
public void testApproveLeaveRequest() {
// 先提交一个请假申请
LeaveRequest request = createTestLeaveRequest();
leaveRequestService.submitLeaveRequest(request);
// 获取已保存的请假申请ID
Student student = studentDAO.findByStudentId(TEST_STUDENT_ID);
int requestId = leaveRequestDAO.findByStudentId(student.getId()).get(0).getId();
// 审批请假申请
boolean result = leaveRequestService.approveLeaveRequest(requestId, ApprovalStatus.APPROVED, "同意请假");
assertTrue("审批请假申请应该成功", result);
// 验证请假申请状态是否更新
LeaveRequest approvedRequest = leaveRequestDAO.findById(requestId);
assertNotNull("应该能找到审批后的请假申请", approvedRequest);
assertEquals("申请状态应该是APPROVED", ApprovalStatus.APPROVED, approvedRequest.getStatus());
assertEquals("审批意见应该匹配", "同意请假", approvedRequest.getApproverComment());
}
@Test
public void testRejectLeaveRequest() {
// 先提交一个请假申请
LeaveRequest request = createTestLeaveRequest();
leaveRequestService.submitLeaveRequest(request);
// 获取已保存的请假申请ID
Student student = studentDAO.findByStudentId(TEST_STUDENT_ID);
int requestId = leaveRequestDAO.findByStudentId(student.getId()).get(0).getId();
// 拒绝请假申请
boolean result = leaveRequestService.approveLeaveRequest(requestId, ApprovalStatus.REJECTED, "请假理由不充分");
assertTrue("拒绝请假申请应该成功", result);
// 验证请假申请状态是否更新
LeaveRequest rejectedRequest = leaveRequestDAO.findById(requestId);
assertNotNull("应该能找到被拒绝的请假申请", rejectedRequest);
assertEquals("申请状态应该是REJECTED", ApprovalStatus.REJECTED, rejectedRequest.getStatus());
assertEquals("审批意见应该匹配", "请假理由不充分", rejectedRequest.getApproverComment());
}
}

View File

@ -0,0 +1,80 @@
package test.service;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import dao.StudentDAO;
import dao.impl.StudentDAOImpl;
import model.Student;
import service.StudentService;
import service.ServiceFactory;
import util.DatabaseUtil;
public class StudentServiceTest {
private StudentService studentService;
private StudentDAO studentDAO;
private static final String TEST_STUDENT_ID = "S2024001";
@Before
public void setUp() throws SQLException {
studentService = ServiceFactory.getStudentService();
studentDAO = new StudentDAOImpl();
// 清理测试数据
cleanTestData();
// 准备测试数据
Student student = createTestStudent();
studentDAO.insert(student);
}
@After
public void tearDown() throws SQLException {
cleanTestData();
}
private void cleanTestData() throws SQLException {
try (Connection conn = DatabaseUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("DELETE FROM students WHERE student_id = ?")) {
stmt.setString(1, TEST_STUDENT_ID);
stmt.executeUpdate();
}
}
private Student createTestStudent() {
Student student = new Student();
student.setStudentId(TEST_STUDENT_ID);
student.setName("测试学生");
student.setClassName("2024级1班");
student.setCollege("计算机学院");
student.setMajor("软件工程");
student.setContact("13900139000");
student.setPassword("password123");
return student;
}
@Test
public void testLoginSuccess() {
Student student = studentService.login(TEST_STUDENT_ID, "password123");
assertNotNull("使用正确的学号和密码应该登录成功", student);
assertEquals("登录后返回的学号应该匹配", TEST_STUDENT_ID, student.getStudentId());
assertEquals("登录后返回的学生姓名应该匹配", "测试学生", student.getName());
}
@Test
public void testLoginFailure() {
Student student = studentService.login(TEST_STUDENT_ID, "wrongPassword");
assertNull("使用错误的密码应该登录失败", student);
}
@Test
public void testLoginWithNonexistentUser() {
Student student = studentService.login("nonexistent", "password123");
assertNull("使用不存在的学号应该登录失败", student);
}
}

View File

@ -0,0 +1,78 @@
package test.service;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import dao.TeacherDAO;
import dao.impl.TeacherDAOImpl;
import model.Teacher;
import service.TeacherService;
import service.ServiceFactory;
import util.DatabaseUtil;
public class TeacherServiceTest {
private TeacherService teacherService;
private TeacherDAO teacherDAO;
private static final String TEST_TEACHER_ID = "T2024001";
@Before
public void setUp() throws SQLException {
teacherService = ServiceFactory.getTeacherService();
teacherDAO = new TeacherDAOImpl();
// 清理测试数据
cleanTestData();
// 准备测试数据
Teacher teacher = createTestTeacher();
teacherDAO.insert(teacher);
}
@After
public void tearDown() throws SQLException {
cleanTestData();
}
private void cleanTestData() throws SQLException {
try (Connection conn = DatabaseUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("DELETE FROM teachers WHERE teacher_id = ?")) {
stmt.setString(1, TEST_TEACHER_ID);
stmt.executeUpdate();
}
}
private Teacher createTestTeacher() {
Teacher teacher = new Teacher();
teacher.setTeacherId(TEST_TEACHER_ID);
teacher.setName("测试教师");
teacher.setDepartment("计算机系");
teacher.setContact("13800138000");
teacher.setPassword("password123");
return teacher;
}
@Test
public void testLoginSuccess() {
Teacher teacher = teacherService.login(TEST_TEACHER_ID, "password123");
assertNotNull("使用正确的工号和密码应该登录成功", teacher);
assertEquals("登录后返回的教师工号应该匹配", TEST_TEACHER_ID, teacher.getTeacherId());
assertEquals("登录后返回的教师姓名应该匹配", "测试教师", teacher.getName());
}
@Test
public void testLoginFailure() {
Teacher teacher = teacherService.login(TEST_TEACHER_ID, "wrongPassword");
assertNull("使用错误的密码应该登录失败", teacher);
}
@Test
public void testLoginWithNonexistentUser() {
Teacher teacher = teacherService.login("nonexistent", "password123");
assertNull("使用不存在的工号应该登录失败", teacher);
}
}