山东大学项目实训(十三)——/query_remain 和 /query_remain1方法的测试

测试 /query_remain 方法

目标

验证GET请求 /query_remain 的功能,确保它能够正确处理查询参数并返回符合条件的工作数据。

测试步骤

  1. 模拟返回数据。
  2. 使用MockMvc发送GET请求。
  3. 验证响应状态码和响应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 的功能,确保它能够正确返回所有工作的数据。

测试步骤

  1. 模拟返回数据。
  2. 使用MockMvc发送GET请求。
  3. 验证响应状态码和响应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

将结果报告给相关后端开发人员进行确认和修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值