I've created working integration test for my application:
@Test
public void postCalendar() throws Exception {
User actualUser = createUser();
actualUser = userRepository.save(actualUser);
String externalId = randomUUID().toString();
Account account = new Account(actualUser, Provider.CUSTOM_PROVIDER, actualUser.getEmail(), externalId);
accountRepository.save(account);
Calendar expectedCalendar = createCalendarWithAccount(account);
mvc.perform(MockMvcRequestBuilders.post("/calendar").contentType(V1_0)
.content(toJSON(createCalendarDto(expectedCalendar))))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(V1_0))
.andExpect(jsonPath("$.id", is(notNullValue())))
.andExpect(jsonPath("$.name", is(expectedCalendar.getName())))
.andExpect(jsonPath("$.title", is(expectedCalendar.getTitle())))
.andExpect(jsonPath("$.description", is(expectedCalendar.getDescription())))
.andExpect(jsonPath("$.color", is(expectedCalendar.getColor())))
.andExpect(jsonPath("$.email", is(expectedCalendar.getEmail())))
.andExpect(jsonPath("$.primary", is(expectedCalendar.isPrimary())));
}
It seems works correctly now. At my first version it was not passed because of ID
field was absent, but I would like use exactly this testing way (next test example doesn't work, it needs to ignore id
fields):
@Test
public void postCalendar() throws Exception {
User actualUser = createUser();
actualUser = userRepository.save(actualUser);
String externalId = randomUUID().toString();
Account account = new Account(actualUser, Provider.CUSTOM_PROVIDER, actualUser.getEmail(), externalId);
accountRepository.save(account);
Calendar expectedCalendar = createCalendarWithAccount(account);
mvc.perform(MockMvcRequestBuilders.post("/calendar").contentType(V1_0)
.content(toJSON(createCalendarDto(expectedCalendar))))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(V1_0))
.andExpect(content().json(toJSON(expectedCalendar)));
}
If my dto
changes - I should not care about changing test criterias, because all json would be compared.
Is it possible to fix this super test with instruction to ignore some fields (id
for example)? How I could declare here to ignore some fields in inner objects (id
for objects in nested collection that can be in content ofCalendar creation)?