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

  • Unit testing with BindException

    Hi,

    I'm writing a unittest for a view class that generates a xml structure of the command object. This xml structure also contains possible binding errors.

    Right now I have a unittest that tests for the correct structure of the xml using a mock command object. I'm trying to find out how I can simulate/mock the BindException Object.

    Any clues?

  • #2
    You don't have to mock a BindException like you would a ServletRequest/Response. You can just instantiate one and pass it to the method you're testing. I do that all the time when I have to test.

    If you need to get into it more, and simulate errors, the BindException class has a whole list of methods you can use to add/list/get errors, etc... check out the API: http://www.springframework.org/docs/...Exception.html

    I'd try to be more specific, but I'm not sure what exactly you need to test.

    Comment


    • #3
      Ill try to explain some more.

      I need to test a method called

      Code:
      view.buildDocument(map,null,null);
      Where buildDocument takes the ModelAndView map as input.

      As everything else is mocked, there is no ServletRequest or anything like it, also no validation has been done.

      You would like to test if the view renders BindingErrors correctly. Therefore I would like to create a BindException myself. I think I'm misunderstanding the constructor of BindException.

      The code that generates the XML for errors I need to test is:

      Code:
      List errors = ex.getFieldErrors(name);
      
      for (Iterator it = errors.iterator(); it.hasNext();) {
      			FieldError error = (FieldError) it.next();
      
      			Element errorElement = new Element("error");
      
      			//add the error message
      			Element errorMessage = new Element("errorMessage");
      			errorMessage.addContent(error.toString());
      			errorElement.addContent(errorMessage);
                           .......
      So I want to fill the FieldErrors of BindException for testing.

      Comment

      Working...
      X