First, test code shouldn't change our develop code. @ModeleAttribute will be mount from your param attribute,so .param() is enough. Below is my demo:
@Test
public void registerUser() throws Exception {
System.out.println("hello......."+rob.toString());
RequestBuilder request = post("/register.html")
.param("username", rob.getUsername())
.param("password", rob.getPassword())
.param("firstName",rob.getFirstName())
.param("lastName",rob.getLastName())
.param("email", rob.getEmail())
.with(csrf());
mvc
.perform(request)
.andDo(MockMvcResultHandlers.print())
.andExpect(redirectedUrl("/"));
}
Then is my Controller.
@Controller
public class LoginController
{
@Autowired
private UserService userService;
@RequestMapping(value = "/remove", method = RequestMethod.GET)
public String removeById(@RequestParam("userid")int id,RedirectAttributes attr)
{
attr.addFlashAttribute("message","remove!!!");
attr.addAttribute("mess","remove ");
return "redirect:/userlist.html";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(@ModelAttribute("user")User user, ModelMap model)
{
System.out.println("register "+user.toString());
boolean result = userService.add(user);
model.addAttribute("message","add "+(result?"successed":"failed")+"!!!");
return "/";
}
}
This can submit right user object to the public String register(@ModelAttribute("user")User user, ModelMap model).
출처) http://stackoverflow.com/questions/27483064/modelattribute-controller-spring-mvc-mocking
'개발 > 웹' 카테고리의 다른 글
javascript garden 번역본 (0) | 2017.10.24 |
---|---|
javascript 내장함수와 closure [펌] (0) | 2017.07.04 |
mybatis typealiases (0) | 2016.11.29 |
spring test mockmvc (0) | 2016.11.29 |
[SPRING-BOOT] controller junit test (0) | 2016.11.29 |
댓글