'Comparing LocalDate using Hamcrest in Junit Test Case

I am my unit testing my REST Controller, one of whose field is LocalDate in my Test Case. Code below:

@Test
 public void getByExternalTransactionId() throws Exception {
        EquityFeeds equityFeeds = new EquityFeeds(423,"SAPEXTXN1", "GS", "ICICI", "BUY", LocalDate.of(2013, 11, 22), 101.9f, "BLO", "Y",0);
        when(equityFeedsService.findByExternalTransactionId("SAPEXTXN1")).thenReturn(equityFeeds);
        mockMvc.perform(MockMvcRequestBuilders.get("/equityFeeds/getByExternalTransactionId/{externalTransactionId}", "SAPEXTXN1"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(jsonPath("$.*", Matchers.hasSize(10)))
                .andExpect(jsonPath("$.id", Matchers.is(423)))
                .andExpect(jsonPath("$.externalTransactionId", Matchers.is("SAPEXTXN1")))
                .andExpect(jsonPath("$.clientId", Matchers.is("GS")))
                .andExpect(jsonPath("$.securityId", Matchers.is("ICICI")))
                .andExpect(jsonPath("$.transactionType", Matchers.is("BUY")))
//                .andExpect(jsonPath("$.transactionDate", Matchers.is(LocalDate.of(2013, 11, 22))))
                .andExpect(jsonPath("$.transactionDate", Matchers.is(LocalDate.of(2013, Month.NOVEMBER, 22))))
//                .andExpect(jsonPath("$.transactionDate", Matchers.is(LocalDateMatchers.isDay(2013, Month.NOVEMBER,22))))
//                .andExpect(jsonPath("$.transactionDate", Matchers.is(LocalDateMatchers.sameDay(LocalDate.of(2013, 11, 22)))))
//                .andExpect(jsonPath("$.transactionDate", Matchers.is(LocalDateMatchers.within(1, ChronoUnit.DAYS, LocalDate.of(2013,11,22)))))
                .andExpect(jsonPath("$.marketValue", Matchers.is(101.9f)))
                .andExpect(jsonPath("$.sourceSystem", Matchers.is("BLO")))
                .andExpect(jsonPath("$.priorityFlag", Matchers.is("Y")))
                .andExpect(jsonPath("$.processingFee", Matchers.is(0)));
        verify(equityFeedsService, times(1)).findByExternalTransactionId("1");
        verifyNoInteractions(equityFeedsService);
    }

Issue:

I have tried Matching LocalDate as per the below code:

.andExpect(jsonPath("$.transactionDate", Matchers.is(LocalDate.of(2013, 11, 22))))
.andExpect(jsonPath("$.transactionDate", Matchers.is(LocalDate.of(2013, Month.NOVEMBER, 22))))

These give me the below Assertion Error:

java.lang.AssertionError: JSON path "$.transactionDate"
Expected: is <2013-11-22>
     but: was <[2013,11,22]>
Expected :is <2013-11-22>
Actual   :<[2013,11,22]>

The other code which I have tried is:

.andExpect(jsonPath("$.transactionDate", Matchers.is(LocalDateMatchers.isDay(2013, Month.NOVEMBER,22))))
.andExpect(jsonPath("$.transactionDate", Matchers.is(LocalDateMatchers.sameDay(LocalDate.of(2013, 11, 22)))))
.andExpect(jsonPath("$.transactionDate", Matchers.is(LocalDateMatchers.within(1, ChronoUnit.DAYS, LocalDate.of(2013,11,22)))))

Each of these is giving me the below exceptions:

java.lang.ClassCastException: class net.minidev.json.JSONArray cannot be cast to class java.time.LocalDate (net.minidev.json.JSONArray is in unnamed module of loader 'app'; java.time.LocalDate is in module java.base of loader 'bootstrap')

How do I compare my LocalDate which is set in the constructor as: LocalDate.of(2013, 11, 22) with the Matchers statement?.



Solution 1:[1]

.andExpect(jsonPath("$.transactionDate", Matchers.is(List.of(2013, 11, 22)))) should work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grez