103. Request with param - dkkahm/study-springfamework5 GitHub Wiki

PostMapping

    @PostMapping("recipe")
    public String saveOrUpdate(@ModelAttribute('recipe') RecipeCommand command) {
        RecipeCommand savedCommand = recipeService.saveRecipeCommand(command);

        return "redirect:/recipe/" + savedCommand.getId() + "/show";
    }

Test

    @Test
    public void testPostNewRecipeForm() throws Exception {
        RecipeCommand command = new RecipeCommand();
        command.setId(2L);
    
        when(recipeService.saveRecipeCommand(any())).thenReturn(command);

        mvcMvc.perform(post("/recipe")
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                    .param("id", "")
                    .param("description", "some string")
                )
                .andExpect(status().is3xxRedirection())
                .andExpect(view().name("redirect:/recipe/2/show"));
    }