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

  • Unit testing with BindException


    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:

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


    • #3
      Ill try to explain some more.

      I need to test a method called

      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:

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