# 一、测试背景 编写此报告的目的在于清楚地阐述l23o6项目中与测试相关的所有内容。包括背景、人员、平台、内容、结果。 ## 1.1目的 为了更好地辅助开发、保证产品的正确性安全性、提高用户信赖程度而进行明确的、有目的的、有效率 的测试。 ## 1.2 参考文献 [1] What is End-to-End (E2E) Testing? All You Need to Know [2] 《软件工程与计算II——软件开发的技术基础》 [3] Chapter Five - Approaches and Tools for Automated End-to-End Web Testing[MaurizioLeotta*, DiegoClerissi*, FilippoRicca*, PaoloTonella†] # 二、测试平台 后端本地测试平台为:MacOS、JDK1.8、JUnit、Postgres数据库(单元测试、集成测试)。 # 三、后端测试内容 ## DAO层测试内容 DAO(Data Access Object)层主要进行的是单元测试。 - 测试形式主要进行黑盒测试(包括边界值测试、随机测试)。 - 测试的主要目的:检验DAO层代码能够完成数据存取的功能。 ### 测试代码示例 ```java @Test public void testFindByRouteIdAndDate() { // 准备测试数据 Long routeId = 123L; String date = "2023-07-07"; List<TrainEntity> trains = new ArrayList<>(); // 添加trainEntity到trains列表 // 模拟trainDao的行为 when(trainDao.findByRouteIdAndDate(routeId, date)).thenReturn(trains); // 调用trainDao的方法 List<TrainEntity> result = trainDao.findByRouteIdAndDate(routeId, date); // 验证返回结果是否符合预期 assertEquals(trains, result); // 验证trainDao的方法是否被调用 verify(trainDao, times(1)).findByRouteIdAndDate(routeId, date); } ``` ### OrderDaoTest | 测试名称 | 输入内容 | 预期结果 | 结果 | | ---------------- | ----------- | ------------------- | ---- | | testFindByUserId | Long userId | 过程中SQL返回值正确 | 通过 | 测试结果截图 ![截屏2023-07-07 下午11.57.22](/Users/wangjunjia/Desktop/截屏2023-07-07 下午11.57.22.png) ### StationDaoTest | 测试名称 | 输入内容 | 预期结果 | 结果 | | -------------- | ----------- | ------------------- | ---- | | testFindByName | String name | 过程中SQL返回值正确 | 通过 | 测试结果截图 ![截屏2023-07-08 上午12.08.52](/Users/wangjunjia/Desktop/截屏2023-07-08 上午12.08.52.png) ### TrainDaoTest | 测试名称 | 输入内容 | 预期结果 | 结果 | | ------------------------ | ------------------------ | ------------------- | ---- | | testFindByDate | String date | 过程中SQL返回值正确 | 通过 | | testFindByName | String name | 过程中SQL返回值正确 | 通过 | | testFindByRouteId | Long routeId | 过程中SQL返回值正确 | 通过 | | testFindByRouteIdAndDate | Long routeId,String date | 过程中SQL返回值正确 | 通过 | 测试结果截图 ![截屏2023-07-07 下午11.41.20](/Users/wangjunjia/Desktop/截屏2023-07-07 下午11.41.20.png) ### UserDaoTest | 测试名称 | 输入内容 | 预期结果 | 结果 | | -------------- | ----------- | ------------------- | ---- | | testFindByName | String name | 过程中SQL返回值正确 | 通过 | 测试结果截图![截屏2023-07-08 上午12.17.05](/Users/wangjunjia/Desktop/截屏2023-07-08 上午12.17.05.png) ## Service层测试 Service层主要进行的是集成测试。 - 测试形式主要进行黑盒测试(包括边界值测试、随机测试)。 - 测试的主要目的:对给定输入进行正确的后端处理,并且能够返回符合预期的输出。 ### RouteServiceImplTest | 测试名称 | 输入内容 | 预期结果 | 测试结果 | | --------------- | ----------------------------- | ------------------------ | -------- | | testListRoutes | null | 返回正确的对象。 | 通过 | | testGetRoute | Long routeId | 返回正确的对象。 | 通过 | | testEditRoute | Long routeId, String newName; | 修改成功 | 通过 | | testDeleteRoute | Long routeId | 删除成功,返回访问成功。 | 通过 | 测试结果截图 ![截屏2023-07-08 上午1.03.55](/Users/wangjunjia/Desktop/截屏2023-07-08 上午1.03.55.png) ### StationServiceImplTest | 测试名称 | 输入内容 | 预期结果 | 测试结果 | | -------------------------------- | ------------------------------ | ---------------- | -------- | | testGetStation | Long stationId | 返回正确的对象。 | 通过 | | testListStations | null | 返回正确的对象。 | 通过 | | testAddStation | String stationName | 添加成功 | 通过 | | testAddStation_StationNameExists | String stationName | 添加成功 | 通过 | | testEditStation | Long stationId, String newName | 修改成功 | 通过 | | testDeleteStation | Long stationId | 删除成功 | 通过 | 测试结果截图 ![截屏2023-07-08 上午1.17.16](/Users/wangjunjia/Desktop/截屏2023-07-08 上午1.17.16.png) ### TrainServiceImplTest | 测试名称 | 输入内容 | 预期结果 | 测试结果 | | ------------------- | --------------------------------------------------- | ---------------- | -------- | | testGetTrain | Long trainID | 返回正确的对象。 | 通过 | | testListTrains | null | 返回正确的对象。 | 通过 | | testListTrainsAdmin | null | 返回正确的对象。 | 通过 | | testAddTrain | String trainName, Long trainID, TrainType trainType | 添加成功 | 通过 | | testDeleteTrain | Long trainID | 删除成功 | 通过 | 测试结果截图 ![截屏2023-07-08 上午1.27.08](/Users/wangjunjia/Desktop/截屏2023-07-08 上午1.27.08.png) ### UserServiceImplTest | 测试名称 | 输入内容 | 预期结果 | 测试结果 | | ---------------------------- | ------------------------------------------------------------ | -------------- | -------- | | testRegister_UsernameExists | String username, String password, String idn, String phone | 注册成功 | 通过 | | testRegister_Success | String username, String password, String role | 注册成功 | 通过 | | testFindByUserName | String username | 返回正确的对象 | 通过 | | testLogin_InvalidCredentials | String username, String password, String role | 登录失败 | 通过 | | testLogin_WrongRole | String username, String password, String role | 登录失败 | 通过 | | testLogin_Success | String username, String password, String role | 登录成功 | 通过 | | testEditInfo_UserNotFound | String username, String name,String idn | 修改失败 | 通过 | | testEditInfo_Success | String username, String name,String idn, String phone, String type | 修改成功 | 通过 | 测试结果截图 ![截屏2023-07-08 上午1.40.56](/Users/wangjunjia/Desktop/截屏2023-07-08 上午1.40.56.png) ## 后端测试结果与结论 所有代码均已根据测试结果进行调试、修改和优化。