M
M
maxvinogradov2021-08-23 18:30:49
Java
maxvinogradov, 2021-08-23 18:30:49

Content Type in testing only needed when submitting body?

Let's say if I want to check how the controller deletes something, why should I set the content type?
Here is the test. This is an example from the Internet, why is contentType needed here, what function does it generally perform? If my understanding is correct, then it reports what the request has in the body, but then the body is empty.

@Test
public void deletePatientById_success() throws Exception {
    Mockito.when(patientRecordRepository.findById(RECORD_2.getPatientId())).thenReturn(Optional.of(RECORD_2));

    mockMvc.perform(MockMvcRequestBuilders
            .delete("/patient/2")
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk());
}

Here is the controller.
@DeleteMapping(value = "{patientId}")
public void deletePatientById(@PathVariable(value = "patientId") Long patientId) throws NotFoundException {
    if (patientRecordRepository.findById(patientId).isEmpty()) {
        throw new NotFoundException("Patient with ID " + patientId + " does not exist.");
    }
    patientRecordRepository.deleteById(patientId);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question