Results 1 to 9 of 9

Thread: Unit testing validators

  1. #1

    Default Unit testing validators

    Hey all,

    I was wondering if anyone had any solutions for unit testing validators withing the Spring Framework architecture?

    Thanks,
    James Winans II

  2. #2
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    The validators are not tied to any specific protocol, so you should be able to instantiate them without any problems.

    Are there any issues you're running into?
    Alef Arendsen
    SpringSource
    http://www.springsource.com

  3. #3

    Default

    Hey,

    The main problem I'm having is dealing with the Errors object in the context of JUnit.
    In setUp() I instantiate three domain objects. One I set with good values. One I set with blank values. And another I fill with bad values. I then pass each of them through my validation object, passing also three BindExceptions I instantiated (errorsGood, errorsBad, & errorsBlank).
    Code:
        private ComputerValidator computerValidator;
        private Computer computerGood, computerBlank, computerBad;
        private BindException errorsGood, errorsBad, errorsBlank;
        
        public void setUp(){
            ComputerValidator computerValidator = new ComputerValidator();
            Computer computerGood = new Computer();
            Computer computerBad = new Computer();
            Computer computerBlank = new Computer();
            BindException errorsGood = new BindException(computerGood, "computerGood");
            BindException errorsBad = new BindException(computerBad, "computerBad");
            BindException errorsBlank = new BindException(computerBlank, "computerBlank");
            computerGood.setRow(29);
            computerGood.setIp("156.152.1.123");
            computerGood.setSeat(3);
            computerGood.setHostname("LOGOS");
            computerGood.setOs("Windows XP");
            computerBad.setRow(42);
            computerBad.setIp("www.cpog.com");
            computerBad.setSeat(11);
            computerValidator.validate(computerGood, errorsGood);
            computerValidator.validate(computerBad, errorsBad);
            computerValidator.validate(computerBlank, errorsBlank);
        }
    Then in my test code I have the following:

    Code:
    public void testValidateInfo(){
            boolean hasErrorsGood = errorsGood.hasErrors();
            boolean hasErrorsBad = errorsBad.hasErrors();
            boolean hasErrorsBlank = errorsBlank.hasErrors();
            assertEquals(true, hasErrorsBlank);
            assertEquals(false, hasErrorsGood);
            assertEquals(false, hasErrorsBad);
        }
    This obviously doesn't work because I get a NullPointerException on "errorsGood.hasErrors();"
    Any help would be great.

    Thanks,
    James Winans

  4. #4
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    I'm confused as to how you could be getting a NullPointerException on

    errorsGood.hasErrors();

    since that method should always run ok:

    public boolean hasErrors() {
    return !this.errors.isEmpty();
    }

    The errors var is set in the constructor, and nothing else could be causing a NPE...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  5. #5
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    You should get a NPE because you've defined local variables in setup. The member variables you are trying to use in testValidateInfo aren't instantiated.

  6. #6

    Default

    You should get a NPE because you've defined local variables in setup. The member variables you are trying to use in testValidateInfo aren't instantiated.
    I guess I need to spend a bit more time with the Java basics. I thought that since I'd declared their reference as an instance variable, outside of setUp(), populating them from within setUp() would have had Class scope as well. :?

  7. #7
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    You've declared the them twice, once as a field, and once as a local var. Just assign to the fields in setup, don't create a variable declaration there too...

  8. #8

    Default

    Ah geesh. I see what I did. :oops: Thanks for the help.

  9. #9

    Default

    Sorry for resurrecting this thread.
    I am interested in the test showed in this thread. I wonder if it is a good unit test for validators (apart of the scope error).
    Is it enough testing the presence of errors or should I test the errors one by one?
    Last edited by David Santoro; Jul 22nd, 2008 at 04:24 PM. Reason: Correcting grammar

Similar Threads

  1. Please help! Unit testing code for JPetStore
    By lakershen in forum Container
    Replies: 4
    Last Post: Jan 13th, 2005, 05:00 PM
  2. AOP and unit testing
    By olivier in forum AOP
    Replies: 2
    Last Post: Jan 10th, 2005, 03:24 PM
  3. Replies: 2
    Last Post: Jan 6th, 2005, 02:49 AM
  4. Problem using hibernate and unit testing
    By rodney.gallart in forum Data
    Replies: 5
    Last Post: Oct 25th, 2004, 12:02 PM
  5. Replies: 2
    Last Post: Aug 26th, 2004, 08:55 AM

Posting Permissions

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