gpt4 book ai didi

java - JUnit 测试 Controller 错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:36 26 4
gpt4 key购买 nike

这是我的 Controller :

@RestController
@RequestMapping(value = Constants.COMPANY, produces = MediaType.APPLICATION_JSON_VALUE)
public class CompanyController {
@Autowired
private CompanyService companyService;

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity find(@ModelAttribute CompanyDto request, @Valid @ModelAttribute PageableRequestModel pageRequest) {
final Page<CompanyDto> dtoPg = companyService.find(request, pageRequest.getPageable());
return new ResponseEntity(PagedResponseDto.of(dtoPg), HttpStatus.OK);
}
}

这是我的 Controller 测试:

  @Test
public void testListAll() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get(COMPANY))
.andExpect(MockMvcResultMatchers.status().isOk());
}

为什么出现错误“嵌套异常是 java.lang.IllegalArgumentException:需要页面!” ??帮助我

最佳答案

    Running controller.CompanyControllerTest
06 Jan 2017 10:02:36,117 INFO: org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping - Mapped "{[/company],methods=[PUT],produces=[application/json]}" onto public org.springframework.http.ResponseEntity<com.kartuku.voucherweb.dto.ResponseDto> com.kartuku.voucherweb.controller.CompanyController.put(java.util.List<com.kartuku.voucherweb.dto.CompanyDto>) At line: 532.
06 Jan 2017 10:02:36,117 INFO: org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping - Mapped "{[/company],methods=[GET],produces=[application/json]}" onto public org.springframework.http.ResponseEntity com.kartuku.voucherweb.controller.CompanyController.find(com.kartuku.voucherweb.dto.CompanyDto,com.kartuku.voucherweb.util.PageableRequestModel) At line: 532.
06 Jan 2017 10:02:36,117 INFO: org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping - Mapped "{[/company],methods=[DELETE],produces=[application/json]}" onto public org.springframework.http.ResponseEntity com.kartuku.voucherweb.controller.CompanyController.delete(java.util.List<com.kartuku.voucherweb.dto.CompanyDto>) At line: 532.
06 Jan 2017 10:02:36,129 INFO: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.test.web.servlet.setup.StubWebApplicationContext@15b5480 At line: 532.
06 Jan 2017 10:02:36,157 INFO: org.springframework.mock.web.MockServletContext - Initializing Spring FrameworkServlet '' At line: 430.
06 Jan 2017 10:02:36,157 INFO: org.springframework.test.web.servlet.TestDispatcherServlet - FrameworkServlet '': initialization started At line: 488.
06 Jan 2017 10:02:36,157 INFO: org.springframework.test.web.servlet.TestDispatcherServlet - FrameworkServlet '': initialization completed in 0 ms At line: 507.
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.16 sec <<< FAILURE! - in controller.CompanyControllerTest
testListAll(controller.CompanyControllerTest) Time elapsed: 0.16 sec <<< ERROR!
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: page required !
at org.springframework.util.Assert.notNull(Assert.java:115)
at com.kartuku.voucherweb.dto.PagedResponseDto.of(PagedResponseDto.java:29)
at com.kartuku.voucherweb.controller.CompanyController.find(CompanyController.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:170)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:137)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:141)
at controller.CompanyControllerTest.testListAll(CompanyControllerTest.java:51)

关于java - JUnit 测试 Controller 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41479929/

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