I want to write a simple test for one of my @RestController
and assert that the input @RequestBody
has been properly mapped to the PersonDTO
:
@RestController
public class PersonServlet {
@PostMapping("/person")
public PersonRsp find(@RequestBody PersonDTO dto) {
//business logic
}
}
public class PersonDTO {
private String firstname, lastname;
}
Question: how can I send a JSON
request body to that servlet. And more over inspect the PersonDTO
fields that all of them have been correctly set?
It's probably similar to this, but I don't know how to inspect/spy the parsed DTO?
@RunWith(SpringRunner.class)
@WebMvcTest(PersonSerlvet.class)
public class PersonTests {
@Autowired
private MockMvc mvc;
@Test
public void testExample() throws Exception {
this.mvc.perform(get("/person"))
.andExpect(status().isOk());
}
}
@Duplicate marker: this is not a duplicate of the linked question (which is about how to read the response body string). I'm actually asking for request body testing.