测试 /query_remain
方法
目标
验证GET请求 /query_remain
的功能,确保它能够正确处理查询参数并返回符合条件的工作数据。
测试步骤
- 模拟返回数据。
- 使用MockMvc发送GET请求。
- 验证响应状态码和响应JSON数据。
测试代码
@Test
public void testQueryRemain() throws Exception {
// 模拟返回数据
Work work1 = new Work("1", "doc1", "2024-06-22", "task1");
Work work2 = new Work("2", "doc1", "2024-06-22", "task2");
List<Work> workList = Arrays.asList(work1, work2);
// 模拟 workdao 的行为
when(workdao.getWork(anyString(), anyString())).thenReturn(workList);
// 发送 GET 请求并验证响应
mockMvc.perform(get("/query_remain")
.param("doc_id", "doc1")
.param("work_day", "2024-06-22"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].id", is("1")))
.andExpect(jsonPath("$[0].doc_id", is("doc1")))
.andExpect(jsonPath("$[0].work_day", is("2024-06-22")))
.andExpect(jsonPath("$[0].task", is("task1")))
.andExpect(jsonPath("$[1].id", is("2")))
.andExpect(jsonPath("$[1].doc_id", is("doc1")))
.andExpect(jsonPath("$[1].work_day", is("2024-06-22")))
.andExpect(jsonPath("$[1].task", is("task2")));
}
mockMvc.perform
:发送GET请求。andExpect(status().isOk())
:验证响应状态码为200。andExpect(jsonPath("$", hasSize(2)))
:验证返回的列表大小。andExpect(jsonPath("$[0].id", is("1")))
等:验证返回的具体内容。
运行结果
执行此测试方法时,控制台会输出测试的执行情况,并验证接口返回的状态码和内容。如果接口实现正确,测试将通过。
测试 /query_remain1
方法
目标
验证GET请求 /query_remain1
的功能,确保它能够正确返回所有工作的数据。
测试步骤
- 模拟返回数据。
- 使用MockMvc发送GET请求。
- 验证响应状态码和响应JSON数据。
测试代码
@Test
public void testQueryRemain1() throws Exception {
// 模拟返回数据
Work work1 = new Work("3", "doc2", "2024-06-23", "task3");
Work work2 = new Work("4", "doc2", "2024-06-23", "task4");
List<Work> workList = Arrays.asList(work1, work2);
// 模拟 workdao 的行为
when(workdao.requeryWork()).thenReturn(workList);
// 发送 GET 请求并验证响应
mockMvc.perform(get("/query_remain1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].id", is("3")))
.andExpect(jsonPath("$[0].doc_id", is("doc2")))
.andExpect(jsonPath("$[0].work_day", is("2024-06-23")))
.andExpect(jsonPath("$[0].task", is("task3")))
.andExpect(jsonPath("$[1].id", is("4")))
.andExpect(jsonPath("$[1].doc_id", is("doc2")))
.andExpect(jsonPath("$[1].work_day", is("2024-06-23")))
.andExpect(jsonPath("$[1].task", is("task4")));
}
mockMvc.perform
:发送GET请求。andExpect(status().isOk())
:验证响应状态码为200。andExpect(jsonPath("$", hasSize(2)))
:验证返回的列表大小。andExpect(jsonPath("$[0].id", is("3")))
等:验证返回的具体内容。
运行结果
执行此测试方法时,控制台会输出测试的执行情况,并验证接口返回的状态码和内容。如果接口实现正确,测试将通过。
在根目录下输入以下代码运行测试:
mvn test
将结果报告给相关后端开发人员进行确认和修改。