Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Spring
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Spring
How to unit test @ModelAttribute in Spring MVC controller
Nitesh Patel
Ranch Hand
Posts: 33
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi All,
I am trying to develop
unit
test for below Spring MVC controller's method using
Junit
but I could not figure out how can I unit test the @ModelAttribute. Can anyone please guide me ?
@RequestMapping(value="addperson.html",method=RequestMethod.POST) public String submitPerson(@ModelAttribute("person") Person person,BindingResult result){ personValidator.validate(person,result); if(result.hasErrors()){ return null; }else{ return "persons"; } }
Regards,
Nitesh
Mark Spritzler
ranger
Posts: 17347
11
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How about in your unit test just doing
Person person = new Person(); // set person values controllerInstance.submitPerson(person, …...
Now the BindingResult object is a different matter. You have to instantiate one or create a mock of one. BindingResult is an interface, and there are implementations. Here is the Javadoc for it
http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/validation/BindingResult.html
Good Luck
Mark
Perfect World Programming, LLC
-
iOS Apps
How to Ask Questions the Smart Way FAQ
Nitesh Patel
Ranch Hand
Posts: 33
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks Mark,
Regards,
Nitesh
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
@Controller annotation query
How can I get a normal input tag's value?
Spring 3 MVC jUnit testing with bindingresult
Integration testing of Spring 2.5 Annotation based controller using web context
@ModelAttribute behavior
More...