使用 PageableDefault 测试 rest 控制器
Testing rest controller with PageableDefault
我正在尝试测试具有 @getmapping 一个参数和 PageableDefault
的休息服务
@GetMapping(path = "/api/{serial}/gender")
public ResponseEntity<?> task(
@PathVariable(value = "serial") String Serial,
@PageableDefault(size = 5, value = 0) Pageable pageable) {
return ResponseEntity.ok(page);
}
带有 mockmvc 的测试套件
@Test
public void testTask() throws Exception {
String url ="/api/123456789/gender";
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(url))
.andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
}
我收到以下错误,我无法注入 Pageable,因为它是一个接口
我如何模拟@pageable 默认值?
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.data.domain.Pageable
吉列尔莫,
试试这段代码:
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.setViewResolvers(new ViewResolver() {
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
return new MappingJackson2JsonView();
}
})
.build();
有关详细信息,请参阅此 post:Isolated Controller Test can't instantiate Pageable
此致!
Pageable in test 可能会导致一些麻烦,尤其是当你使用一些额外的工具如 swagger 或本地数据库只是为了像 H2 那样品尝时。
首先我们必须添加我们要测试的控制器:
@Autowired
private TestingController testingController;
没有 Pagable 的控制器的简单测试如下所示:
given().contentType(ContentType.JSON).log().all().
standaloneSetup(testingController).
body(new RequestObjectExample()).
when().
post("/someUrl?page=0&size=10,asc").
then().statusCode(200);
带有 Pagable 的控制器的简单测试如下所示:
StandaloneMockMvcBuilder standaloneMockMvcBuilder = MockMvcBuilders.standaloneSetup(testingController)
.setCustomArgumentResolvers(pageableArgumentResolver);
given().contentType(ContentType.JSON).log().all().
standaloneSetup(standaloneMockMvcBuilder).
body(new RequestObjectExample()).
when().
post("/someUrl?page=0&size=10,asc").
then().statusCode(200);
P.S。要获得 pageableArgumentResolver 只需导入 PageableHandlerMethodArgumentResolver 并将其注入 class:
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
@Inject
private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
我正在尝试测试具有 @getmapping 一个参数和 PageableDefault
的休息服务 @GetMapping(path = "/api/{serial}/gender")
public ResponseEntity<?> task(
@PathVariable(value = "serial") String Serial,
@PageableDefault(size = 5, value = 0) Pageable pageable) {
return ResponseEntity.ok(page);
}
带有 mockmvc 的测试套件
@Test
public void testTask() throws Exception {
String url ="/api/123456789/gender";
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(url))
.andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
}
我收到以下错误,我无法注入 Pageable,因为它是一个接口 我如何模拟@pageable 默认值?
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.data.domain.Pageable
吉列尔莫,
试试这段代码:
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.setViewResolvers(new ViewResolver() {
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
return new MappingJackson2JsonView();
}
})
.build();
有关详细信息,请参阅此 post:Isolated Controller Test can't instantiate Pageable
此致!
Pageable in test 可能会导致一些麻烦,尤其是当你使用一些额外的工具如 swagger 或本地数据库只是为了像 H2 那样品尝时。
首先我们必须添加我们要测试的控制器:
@Autowired
private TestingController testingController;
没有 Pagable 的控制器的简单测试如下所示:
given().contentType(ContentType.JSON).log().all().
standaloneSetup(testingController).
body(new RequestObjectExample()).
when().
post("/someUrl?page=0&size=10,asc").
then().statusCode(200);
带有 Pagable 的控制器的简单测试如下所示:
StandaloneMockMvcBuilder standaloneMockMvcBuilder = MockMvcBuilders.standaloneSetup(testingController)
.setCustomArgumentResolvers(pageableArgumentResolver);
given().contentType(ContentType.JSON).log().all().
standaloneSetup(standaloneMockMvcBuilder).
body(new RequestObjectExample()).
when().
post("/someUrl?page=0&size=10,asc").
then().statusCode(200);
P.S。要获得 pageableArgumentResolver 只需导入 PageableHandlerMethodArgumentResolver 并将其注入 class:
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
@Inject
private PageableHandlerMethodArgumentResolver pageableArgumentResolver;