I'm trying to run a test on my Spring Boot endpoint, which accepts is supposed to take information from a client-side form
, map the input fields to the DTO and persists it into the DB, but I can't get the test schema to accept it. From my understanding, when you have a controller endpoint defined like this:
@PostMapping(path = "/newContact")
public @ResponseBody ContactDTO createNewContact(
@ModelAttribute ContactDTO newContact) {
//persists newContact to the data tier
}
the @ModelAttribute tag will automatically search for the name of the fields of the newContact
DTO in the incoming JSON name, and then map the json values to fill the fields of the DTO.
Here is my ContactDTO class:
public class ContactDTO {
private BigInteger userId;
private BigInteger contactId;
private String name;
private String email;
private String bday;
private String address;
private String city;
private String state;
private List<PhoneDTO> phones;
private MultipartFile contactImgUpload;
//getters and setters
}
Firstly, is this a correct understanding?
So, I'm trying to test that my endpoint works, by making the expected DTO, but converting it to JSON, and then POSTing that JSON to the controller endpoint:
@Autowired
ObjectMapper objectMapper;
@Test
public void saveAnEntryWhenPOSTUserWithMultiplePhones() throws Exception {
List<PhoneDTO> phones = new ArrayList<>();
phones.add(new PhoneDTO("landline", "12312312312"));
phones.add(new PhoneDTO("mobile", "3242523462"));
ContactDTO cDTO = new ContactDTO();
cDTO.setContactId(BigInteger.valueOf(555));
cDTO.setName("Multiphone User");
cDTO.setUserId(BigInteger.valueOf(123));
cDTO.setEmail("test@email.com");
cDTO.setBday("01/01/1987");
cDTO.setState("IL");
cDTO.setCity("Chicago");
cDTO.setAddress("55 Jackson");
cDTO.setPhones(phones);
this.mockMvc.perform(post("/newContact")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(cDTO)))
.andExpect(status().isOk())
.andExpect(content().string(containsString("\"phoneType\":\"landline:\"")));
}
But when I do this, it is clearly not sending the JSON in the expected way, as it fails when it attempts to save to the data tier, saying that some of the expected fields which are expected to be populated (here the "name" field) are empty:
org.springframework.web.util.NestedServletException: Request processing failed;
nested exception is javax.validation.ConstraintViolationException:
Validation failed for classes [app.models.relationentities.Contact] during
persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='may not be null',
propertyPath=name, rootBeanClass=class app.models.relationentities.Contact, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]
So, am I wrong with the test? How do I mock sending a filled-out form to the test?
Edit
Including the phoneDTO, which is what the ContactDTO field private List<PhoneDTO> phones;
holds a list of:
public class PhoneDTO {
private String phoneType;
private String number;
//getters and setters
}