Announcement Announcement Module
No announcement yet.
JUnit testing Spring Controllers and Validators Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • JUnit testing Spring Controllers and Validators

    Are there any built in classes/standard methodology for using JUnit to test Spring Controllers (e.g. SimpleFormController) and validator classes? Some help, or even a pointer in the right director would help a lot.

  • #2
    Any of the test superclasses in org.springframework.test can be used. The controllers are just another class. Its just a matter of setting up your mock objects appropriately. The book Pro Spring covers testing topics, too - a good book to have.


    • #3
      Thanks for that, and I do actually already have that book.

      What I'm having problems with now are with testing controllers with JUnit. How can I perform a post to my controller to test if it's doing the correct things?

      I have a web page and a command object. The command object (RefCodeManager) has a property containing a list of Ref Codes. There are two buttons at the bottom of the page, 'Add' and 'Update'. 'Add' simply adds another blank 'ref code' to the list of ref codes in the command object and then reloads the page. 'Update' takes the command object and the ref codes in it and inserts/updates them to the database.

      The problem comes when testing that the 'Add' and 'Update' submit buttons are doing their job. Here's what I have to test the add button. But even with out the request.addParameter("add", "") it still seems to work?!?

      public void testAdd() throws Exception {
      RefCodeManager origRcm = (RefCodeManager) getBean("testingRefCodeManager");

      request = newPost("/refcodeform.html");
      request.addParameter("add", "");
      mv = controller.handleRequest(request, new MockHttpServletResponse());
      Map model = mv.getModel();
      RefCodeManager newRcm = (RefCodeManager)model.get(controller.getCommandNam e());
      assertEquals(origRcm.getRefCodes().size() + 1, newRcm.getRefCodes().size());

      Can anyone help?