- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下测试代码,我正在测试可分页端点,其中列出了学生的所有条目。
@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/
我尝试在我的 Controller 中测试我的方法 addPerson(),但是当我执行测试时,我在 MockHttpServletResponse 中有一个空主体的状态 200。我想用 MockMv
我尝试使用 sprint-test 为 Servlet 编写单元测试模拟对象 我的 Maven 依赖项是: org.springframework spring-test 4.0.
我有以下测试代码,我正在测试可分页端点,其中列出了学生的所有条目。 @Autowired private MockMvc mockMvc; @MockBean private StudentR
I have written unit test cases for a spring rest controller but I'm getting blank response body. 下面是
我注意到在 Spring Boot 测试中,MockHttpServletResponse 的 getContentLength() 与 javax.servlet.http.HttpServletR
我正在使用 spring MockHttpServletResponse 和请求为 servlet 过滤器编写测试。创建 MockHttpServletResponse 对象时出现以下错误。 Caus
在编写 Spring Itegration 测试时,我遇到了 MockMvc 忽略了我的问题 .accept(MediaType.APPLICATION_JSON_UTF8) 设置,并返回带有难看变音
我是一名优秀的程序员,十分优秀!