PDA

View Full Version : Unit testing validators



jpwinans
Oct 20th, 2004, 12:52 PM
Hey all,

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

Thanks,
James Winans II

Alef Arendsen
Oct 20th, 2004, 04:33 PM
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?

jpwinans
Oct 20th, 2004, 05:42 PM
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).


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:


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

Colin Sampaleanu
Oct 23rd, 2004, 07:30 AM
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...

katentim
Oct 23rd, 2004, 05:17 PM
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.

jpwinans
Oct 25th, 2004, 10:44 AM
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. :?

Colin Sampaleanu
Oct 25th, 2004, 11:32 AM
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...

jpwinans
Oct 25th, 2004, 06:13 PM
Ah geesh. I see what I did. :oops: Thanks for the help.

David Santoro
Jul 22nd, 2008, 04:21 PM
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?