Announcement Announcement Module
Collapse
No announcement yet.
testing form controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • testing form controller

    Hello

    For Controllers that extend Controller class, I can test the handleRequest function such as:

    public void testHandleRequest throws ServerException.....


    What will I have to do to test with the classes that extend the SimpleFormController. I think that I donot have to test ffor functions like onSubmit, onBackingObject.....). For example, I have in my form

    public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors){
    MyObject myObject = (MyObject)command;
    myObjectManager.add(myObject);
    return new ModelAndView(new RedirectView(getSuccessView()));
    }

    How I can test this

    Thank you for any help
    SHo
    Last edited by shoa; Apr 19th, 2006, 12:07 AM.

  • #2
    Originally posted by shoa
    What will I have to do to test with the classes that extend the SimpleFormController. I think that I donot have to test ffor functions like onSubmit, onBackingObject.....).
    You should only test *your* code. If you have overridden any of those methods, sure, test them, but don't bother testing Spring's code .

    In the same token, you shouldn't really be testing the workflow of a class, just it's individual methods.

    Originally posted by shoa
    For example, I have in my form

    public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors){
    MyObject myObject = (MyObject)command;
    myObjectManager.add(myObject);
    return new ModelAndView(new RedirectView(getSuccessView()));
    }
    Simply test the method in isolation:

    Code:
      MyObject myCommand = new MyObject();
    
      MyController controller = new MyController();
    
      ModelAndView mav = controller.onSubmit(myCommand);
      View view = mav.getView();
      assertTrue("view is type redirect", RedirectView.class, view.getClass());
      etc.
    Note I am using the onSubmit(Object) method, not your method which takes in all the parameters. If you do want to test that, then you can create MockHttpServletRequest and MockHttpServletResponse etc.

    Remember, it is much better to have lots of little tests that a single aspect...

    HTH

    Comment

    Working...
    X