// Laat Spring enkel de relevante controller bouwen en geeft ons de MockMvc helper
@WebMvcTest(controllers = GameController.class)
public class GameControllerTest {
@Autowired
private MockMvc mvc;
private static final ObjectMapper mapper = new ObjectMapper();
@Test
public void create() throws Exception {
Map<String, Object> requestBody = Map.of(
"id", 5, // geef de id mee in de request body zodat de code deze er uit moet halen
"title", "Dark Souls",
"releaseDate", "2011-09-22",
"developer", "FromSoftware");
mvc.perform(post("/games")
.contentType(APPLICATION_JSON)
.content(mapper.writeValueAsString(requestBody)))
.andExpectAll(
// We verwachten een 201 status
status().isCreated(),
// en de locatie van de nieuwe resource in de headers
header().string(HttpHeaders.LOCATION, "/games/5"));
}
}
view raw SpringTest.java hosted with ❤ by GitHub