弹簧休息测试@JsonFormat注释

时间:2018-02-25 16:53:07

标签: spring rest testing

我需要测试我的端点/人员是否返回正确的json值。 我在我的Person模型`@JsonFormat注释中使用,当我使用Postman或浏览器时,它以“dd-MM-yyyy”格式生成正确的日期。

示例:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyy")
private LocalDate birthDate;

输出在json->中是正确的。 birthDate: "01-01-2000"

但是,如果我想测试我的控制器,它会产生以下错误:

Expected: is "01-01-2000"
 but: was <{year=2000, month=JANUARY, monthValue=1, dayOfMonth=1, 
 chronology={id=ISO, calendarType=iso8601}, leapYear=true, 
 dayOfWeek=SATURDAY, dayOfYear=1, era=CE}>

我不知道问题出在哪里。 我的测试方法如下所示:

//given
    Person testPerson = new Person("Jan", "Kowalski", LocalDate.of(2000, 1, 1),
            LocalDate.of(2005, 12, 31), "123456");
    List<Person> peopleList = Arrays.asList(testPerson);

    //when
    Mockito.when(personRepository.findAll()).thenReturn(peopleList);

    //then
    mockMvc.perform(get("/people"))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].birthDate", Matchers.is("01-01-2000"))));

提前致谢!

0 个答案:

没有答案