Mam metodę w kontrolerze, która odbiera nowe dane do aktualizacji profilu użytkownika. W parametrze tej metody jest dto, który ma np. imię, nazwisko i zdjęcie profilowe, czyli MultipartFile.
@PatchMapping("/settings/profile")
@ResponseStatus(HttpStatus.NO_CONTENT)
void updateUserProfile(@Valid @RequestBody AppUserProfileEditDTO userProfile) {
appUserService.updateUserProfile(currentUserFacade.getCurrentUser(), userProfile);
}
Napisałem sobie taki test jak na screenie, w sekcji given robię zapis użytkownika do bazy danych i tworzę ten dto z nowymi danymi. Linia 281 wyrzuca wyjątek InvalidDefinitionException.
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: pl.secret.iteventsapi.appuser.domain.dto.AppUserProfileEditDTO["profileImage"]->org.springframework.mock.web.MockMultipartFile["inputStream"])
@Test
@Transactional
@WithMockUser(username = "jankowalski@example.com")
void shouldReturnUpdatedUserProfile() throws Exception {
// given
AppUser user = AppUserCreator.create("Jan", "Kowalski", imageRepository.save(ProfileImageCreator.createDefaultProfileImage()));
appUserRepository.save(user);
AppUserProfileEditDTO newUserProfileData = AppUserProfileEditDTOCreator.create(
user,
"Kraków",
"Cześć!");
// when
MockHttpServletRequestBuilder request = MockMvcRequestBuilders
.patch("/api/v1/settings/profile")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(newUserProfileData));
mockMvc.perform(request)
.andDo(print())
.andExpect(status().isNoContent());
// then
AppUser userAfterUpdate = appUserRepository.findById(user.getId()).get();
assertThat(userAfterUpdate.getCity()).isEqualTo(newUserProfileData.getCity());
assertThat(userAfterUpdate.getBio()).isEqualTo(newUserProfileData.getBio());
}
Wiem, że tutaj jest problem z serializacją pliku. Jak powinno się tworzyć takie dto w testach i jak napisać test dla takiej metody
```
.