使用 MockMvc 和 JUnit 5 测试 LocalDate 时出现 AssertionError

AssertionError when testing LocalDate with MockMvc and JUnit 5

我正在尝试在 Spring Boot 2.3 项目中将 JUnit 5 与 Hibernate 5.4 和 MockMvc 结合使用。

我的 employee 实体 class 的成员是这样的:

import java.time.LocalDate;
... (Rest of the imports)

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long employeeId;

    @Column(nullable = false, updatable = true)
    private String firstName;

    @Column(nullable = true, updatable = true)
    private String lastName;

    @Column(nullable = false, updatable = true)
    private String email;

    @Column(nullable = false, updatable = true)
    private LocalDate birthDate;

我正在尝试使用 JUnit 5 使用 Mockito 和 MockMvc 测试 LocalDate 字段 birthDate。这是测试用例的样子:

    @Test
    public void testFindEmployeeById() throws Exception {
        Employee mockedEmployee = makeEmployee(9L, "Mock First", "Mock Last", "mock@email.com",
                LocalDate.of(1996, 9, 8), 9L, "Mock Project", "Mock Department");

        Mockito.when(employeeRepositoryMock.findById(mockedEmployee.getEmployeeId()))
                .thenReturn(Optional.of(mockedEmployee));

        mockMvc.perform(MockMvcRequestBuilders.get("/v1/employees/id/9").accept(MediaType.APPLICATION_JSON))
                .andDo(MockMvcRestDocumentation.document("employeeById",
                        Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
                        PayloadDocumentation.responseFields(
                                PayloadDocumentation.fieldWithPath("employeeId").description("Employee ID"),
                                PayloadDocumentation.fieldWithPath("firstName").description("First Name"),
                                PayloadDocumentation.fieldWithPath("lastName").description("Last Name"),
                                PayloadDocumentation.fieldWithPath("email").description("Email Address"),
                                PayloadDocumentation.fieldWithPath("birthDate").description("Date of Birth"),
                                PayloadDocumentation.fieldWithPath("project.projectId").description("Project ID"),
                                PayloadDocumentation.fieldWithPath("project.name").description("Project name"),
                                PayloadDocumentation.fieldWithPath("project.department.departmentId")
                                        .description("Department ID"),
                                PayloadDocumentation.fieldWithPath("project.department.name")
                                        .description("Department Name"))))
                .andDo(print()).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.employeeId").value(mockedEmployee.getEmployeeId()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.firstName").value(mockedEmployee.getFirstName()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.lastName").value(mockedEmployee.getLastName()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.email").value(mockedEmployee.getEmail()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.birthDate").value(mockedEmployee.getBirthDate()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.project.projectId")
                        .value(mockedEmployee.getProject().getProjectId()))
                .andExpect(
                        MockMvcResultMatchers.jsonPath("$.project.name").value(mockedEmployee.getProject().getName()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.project.department.departmentId")
                        .value(mockedEmployee.getProject().getDepartment().getDepartmentId()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.project.department.name")
                        .value(mockedEmployee.getProject().getDepartment().getName()));
    }

在 运行 测试用例上,我得到一个 AssertionError:

java.lang.AssertionError: JSON path "$.birthDate" expected:<1996-09-08> but was:<1996-09-08>

如您所知,两个字符串表示形式相同。然而 JUnit 5 抛出错误。

那么,比较 LocalDate 与 JUnit 5 的正确方法是什么?

可以在这里找到类似的问题:Comparing LocalDate using Hamcrest in Junit Test Case 在这里:

提前致谢。 :)

JsonPath 在实际 JSON 本身上运行。此时,您的 LocalDate 已转换为 JSON string 值。虽然有一个论点认为匹配器应该自动将 Java 值转换为字符串,但它不会,但是如果你调用 localDate.toString(),那么它应该可以工作。

(更一般地说,任何时候你收到一个匹配器错误,指出 看起来 相同的两个值不匹配,这可能是因为它们是具有共同点的不同类型toString() 格式。)