1

I have a test method, where I would like to send Date values as url params, and then in cotroller to manipulate with such a data.

Test method:

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

Controller:

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

I have based on two stack articles: how to pass date via url parameter - junit test with dates and How to accept Date params in a GET request to Spring MVC Controller?

But none worked properly. Can you help me out with this, please? I can even send Long values to controller as param if it is simplier

xross
  • 597
  • 4
  • 9
  • 25
  • explain non worked properly – pvpkiran Nov 21 '17 at 12:51
  • I am putting breakpoints on the controller method, and there is no action(stop) during debugging run of this test – xross Nov 21 '17 at 12:55
  • like it is not even going into this method. I am recieving 404 – xross Nov 21 '17 at 12:57
  • have you tried DateTime or LocalDateTime rather than Date as you are requested a date time format not a date – Amr Alaa Nov 21 '17 at 13:00
  • Your controller method takes 4 request params but in your test case you are not sending anything? how would this work. – pvpkiran Nov 21 '17 at 13:02
  • 2
    @pvpkiran they are all required false so its ok – Amr Alaa Nov 21 '17 at 13:09
  • @AmrAlaa I didnt try, I will in a second. A while ago I wrote the same case but instead Date I passed Long value and then new Date(Long), and it didint worked as well (its behaviour was the same as it is now) – xross Nov 21 '17 at 13:15
  • @pvpkiran if there is no parameter there should be null value, and I can handle this case (another tests without using date is passing ok) – xross Nov 21 '17 at 13:16
  • did your try changing the dateformat? does it work? – pvpkiran Nov 21 '17 at 13:17
  • I didnt, but it is required for me to have a very similiar dateformat as it is now, because in DB there is TimeStamp and I want DB to filter that throught date (I am updating my post with another methods to give you a wider view) – xross Nov 21 '17 at 13:20
  • you DateTimeFormat is Spring DateTimeFormat or Joda DateTimeFormat? – pvpkiran Nov 21 '17 at 13:20
  • I think it is spring, but it is written jodatime...Sorry I am giving you details this way, I dont want to make any doubts about that: https://www.dropbox.com/s/p1py9q2oe6my26v/Przechwytywanie_Date.PNG?dl=0 – xross Nov 21 '17 at 13:24
  • Probably an aside, just thinking it would be natural to use ISO 8601 format for your date-times when passing them in a URL. I believe Joda-Time produces this format easily. – Ole V.V. Nov 21 '17 at 13:24

0 Answers0