Issue
I am trying to use JUnit 5 with Hibernate 5.4 and MockMvc in a Spring Boot 2.3 project.
This is how the members of my employee
entity class looks like:
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;
I am trying to test the LocalDate field birthDate with Mockito and MockMvc using JUnit 5. This is how the test case for that looks like:
@Test
public void testFindEmployeeById() throws Exception {
Employee mockedEmployee = makeEmployee(9L, "Mock First", "Mock Last", "[email protected]",
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()));
}
Upon running the test case, I get an AssertionError:
java.lang.AssertionError: JSON path "$.birthDate" expected:<1996-09-08> but was:<1996-09-08>
As you can tell, both the string representations are the same. Yet JUnit 5 throws the error.
So, what is the right way to go about comparing LocalDate with JUnit 5?
A similar question can be found here: Comparing LocalDate using Hamcrest in Junit Test Case and here: JUnit AssertionError when testing SimpledateFormat
Thanks in advance. :)
Solution
JsonPath operates on actual JSON itself. By this time, your LocalDate
has been converted to a JSON string
value. While there's an argument that the matcher should automatically convert Java values to strings, it doesn't, but if you call localDate.toString()
, then you it should work.
(More generally, any time you get a matcher error saying that two values that look identical don't match, it's probably because they're different types that have a common toString()
format.)
Answered By - chrylis -cautiouslyoptimistic-