我有一个测试方法,我希望将Date值作为url params发送,然后在cotroller中使用这样的数据进行操作。
测试方法:
@Test
public void getUsageCountersParam2Test() throws Exception {
String date = "2017-06-23 12:39:00";
MvcResult result = mockMvc.perform(get("/usages?dateFrom=" + date)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
ObjectMapper mapper = new ObjectMapper();
List<UsageCounterDetailsDto> list = mapper.readValue(result.getResponse().getContentAsString(), new TypeReference<List<UsageCounterDetailsDto>>() {
});
assertNotNull(list);
assertEquals(3, list.size());
}
控制器:
@RequestMapping(method = RequestMethod.GET)
public List<UsageCounterDetailsDto> getUsageCounters(
@RequestParam(name = "apiConsumerIdsList", required = false) List<Integer> apiConsumerIdsList,
@RequestParam(name = "serviceIdsList", required = false) List<Integer> serviceIdsList,
@RequestParam(name = "dateFrom", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date dateFrom,
@RequestParam(name = "dateTo", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date dateTo,
@RequestParam(name = "status", required = false) UsageCounterDetailsDto.Status status
) {
return counterService.getUsageCountersList(apiConsumerIdsList, serviceIdsList, dateFrom, dateTo, status);
}
我基于两篇文章: how to pass date via url parameter - junit test with dates 和 How to accept Date params in a GET request to Spring MVC Controller?
但没有一个正常工作。你可以帮我解决这个问题吗? 如果它更简单,我甚至可以将Long值作为参数发送给控制器