Results 1 to 2 of 2

Thread: testing form controller

  1. #1
    Join Date
    Apr 2006
    Posts
    166

    Default 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 18th, 2006 at 11:07 PM.

  2. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Quote 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.

    Quote 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •