Сравнение LocalDate с использованием Hamcrest в тестовом примере Junit - PullRequest
0 голосов
/ 13 апреля 2020

Я тестирую мой REST-контроллер, одним из полей которого является LocalDate в моем тестовом примере. Код ниже:

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

Проблема:

Я пробовал Matching LocalDate согласно приведенному ниже коду:

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

Это дает мне следующую ошибку подтверждения:

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]>

Другой код, который я пробовал:

.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)))))

Каждый из них дает мне следующие исключения:

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')

Как мне сравнить мой LocalDate который установлен в конструкторе как: LocalDate.of (2013, 11, 22) с оператором Matchers?.

...