gpt4 book ai didi

java - MockHttpServletResponse 返回 Pageable 端点的空主体

转载 作者:行者123 更新时间:2023-12-01 17:29:59 35 4
gpt4 key购买 nike

我有以下测试代码,我正在测试可分页端点,其中列出了学生的所有条目。

    @Autowired
private MockMvc mockMvc;

@MockBean
private StudentRepository studentRepository;

private PageableHandlerMethodArgumentResolver pageableArgumentResolver = new PageableHandlerMethodArgumentResolver();

@BeforeEach
public void init() {
mockMvc = MockMvcBuilders.standaloneSetup(new StudentEndpoint(studentRepository))
.setCustomArgumentResolvers(pageableArgumentResolver)
.build();
}

@Test
@WithMockUser(username = "xx", password = "xx", roles = "USER")
public void whenListStudentUsingCorrectStudentUsernameAndPassword_thenReturnStatusCode200 () throws Exception {
List<Student> students = asList(new Student(1L, "Legolas", "legolas@lotr.com"),
new Student(2L, "Aragorn", "aragorn@lotr.com"));

when(studentRepository.findAll()).thenReturn(students);


mockMvc.perform(get("http://localhost:8080/v1/protected/students/"))
.andExpect(status().isOk())
.andDo(print());

verify(studentRepository, times(1)).findAll();
}

这里的问题是 verify(studentRepository, times(1)).findAll(); 不起作用,因为 MockHttpServletResponse 返回 null Body。

这就是我的终点:

    @GetMapping(path = "protected/students")
public ResponseEntity<?> listAll (Pageable pageable) {
return new ResponseEntity<>(studentDAO.findAll(pageable), HttpStatus.OK);
}

还有我的日志:

   MockHttpServletResponse:
Status = 200
Error message = null
Headers = []
Content type = null
Body =
Forwarded URL = null Redirected URL = null
Cookies = []

Argument(s) are different! Wanted:
br.com.devdojo.repository.StudentRepository#0 bean.findAll(

);
-> at br.com.devdojo.TestingTestTech.whenListStudentUsingCorrectStudentUsernameAndPassword_thenReturnStatusCode200(TestingTestTech.java:68)

Actual invocations have different arguments:

br.com.devdojo.repository.StudentRepository#0 bean.findAll(

Page request [number: 0, size 20, sort: UNSORTED]
);

有人可以帮助测试可分页响应的正确方法吗?谢谢。

最佳答案

最后,我找到了解决方法。

您只需将 Pageable 对象作为参数传递给返回 Pageable JSON 的 findAll 方法。

这是我的新工作代码:

        Page<Student> pagedStudents = new PageImpl(students);

when(studentRepository.findAll(isA(Pageable.class))).thenReturn(pagedStudents);


mockMvc.perform(get("http://localhost:8080/v1/protected/students/"))
.andExpect(status().isOk())
.andDo(print());

verify(studentRepository).findAll(isA(Pageable.class));

以及 MockHttpServletResponse:

    MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json"]
Content type = application/json
Body = {"content":[{"id":1,"name":"Legolas","email":"legolas@lotr.com"},{"id":2,"name":"Aragorn","email":"aragorn@lotr.com"}],"pageable":"INSTANCE","totalElements":2,"totalPages":1,"last":true,"size":2,"number":0,"sort":{"sorted":false,"unsorted":true,"empty":true},"first":true,"numberOfElements":2,"empty":false}
Forwarded URL = null
Redirected URL = null
Cookies = []

关于java - MockHttpServletResponse 返回 Pageable 端点的空主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61140964/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com