【问题标题】:JUnit test controller errorJUnit 测试控制器错误
【发布时间】:2017-01-05 08:00:19
【问题描述】:

这是我的控制器:

@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());  
   } 

为什么错误'嵌套异常是 java.lang.IllegalArgumentException: page required !' ??帮帮我

【问题讨论】:

  • 您是如何设置测试的?它的配置是什么?能否请您发布完整的堆栈跟踪?

标签: java unit-testing spring-mvc junit controller


【解决方案1】:
    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)

【讨论】:

    【解决方案2】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 2021-08-08
      • 2012-01-04
      • 2023-03-10
      相关资源
      最近更新 更多