JUnit测试控制器错误

时间:2017-01-05 08:00:20

标签: java unit-testing spring-mvc junit 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);
}
}

这是我的控制器测试:

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

为什么错误&#39;嵌套异常是java.lang.IllegalArgumentException:需要页面!&#39; ??帮助我

2 个答案:

答案 0 :(得分:0)

    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)

答案 1 :(得分:0)

您传递给PagedResponseDto.of(...)方法的Paging值似乎无效。

这是在抛出NestedServletException之前来自堆栈跟踪中代码的最后一行:

at com.kartuku.voucherweb.dto.PagedResponseDto.of(PagedResponseDto.java:29)

调试测试并检查代码中的值。

您需要适当地模拟CompanyService并调用CompanyService.find(...)(使用类似Mockito之类的内容)或以某种方式提供CompanyService的实际实例。