Results 1 to 3 of 3

Thread: writing Junit tests for validator implementations

  1. #1
    Join Date
    Sep 2005
    Posts
    2

    Default writing Junit tests for validator implementations

    Hi, I'm looking for an example how to call a validator implementation (PersonValidator) outside of a web application context and form controller (AddPersonController). I'm trying to write a test class that would excersize this PersonValidator class and verify my rules work the way I intended them to. Given:

    Code:
    public class Person {
      private String name;
      private int age;
    
      // the usual suspects: getters and setters
    }
    and

    Code:
    public class PersonValidator implements Validator {
    	
    	public boolean supports(Class clzz) {
    		return Person.class.equals(clzz);
    	}
    	
    	public void validate(Object obj, Errors e) {
    		ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
    		Person p = (Person)obj;
    		if &#40;p.getAge&#40;&#41; < 0&#41; &#123;
    			e.rejectValue&#40;"age", "negativevalue"&#41;;
    		&#125; else if &#40;p.getAge&#40;&#41; > 110&#41; &#123;
    			e.rejectValue&#40;"age", "tooold"&#41;;
    		&#125;
    	&#125;
    &#125;
    What might my PersonValidatorTest class look like?

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

    Default

    Just typed this up without testing, but this should get you started:
    Code:
        public void testNegativeAgeRejected&#40;&#41; &#123;
    		Person person = new Person&#40;&#41;;
    		person.setAge&#40;-1&#41;;
    
    		Errors errors = new BindException&#40;person, "person"&#41;;
    		
    		PersonValidator pv = new PersonValidator&#40;&#41;;
    		pv.validate&#40;person, errors&#41;;
    
    		assertTrue&#40;errors.hasFieldErrors&#40;"age"&#41;&#41;;
    		assertEquals&#40;"negativevalue", errors.getFieldError&#40;"age"&#41;.getCode&#40;&#41;&#41;;
        &#125;

  3. #3

    Default

    To add to this, let me extend this question...

    How do you unit test a Validator that delegates to another Validator?

    For example, assuming Person had an Address object...

    Code:
    public class PersonValidator implements Validator {
    	
    	public boolean supports(Class clzz) {
    		return Person.class.equals(clzz);
    	}
    	
    	public void validate(Object obj, Errors e) {
    		ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
    		Person p = (Person)obj;
    		if (p.getAge() < 0) {
    			e.rejectValue("age", "negativevalue");
    		} else if (p.getAge() > 110) {
    			e.rejectValue("age", "tooold");
    		}
    		
    		e.pushNestedPath("address");
    		ValidationUtils.invokeValidator( new AddressValidator(), obj.getAddress(), e);
    		e.popNestedPath();
    	}
    }
    How can I unit test PersonValidator, mocking AddressValidator's result, given that AddressValidator is instantiated within PersonValidator? The only thing I can think of is to make AddressValidator an injectable property, but I haven't seen people doing that in all of the Validator examples on this forum.

    Thanks!
    Leo

Similar Threads

  1. JUnit tests - Support multiple databases
    By ejhernand3z in forum Data
    Replies: 5
    Last Post: Oct 22nd, 2008, 02:41 PM
  2. How to turn off Spring logging when in JUnit?
    By dhicks in forum Container
    Replies: 7
    Last Post: Apr 23rd, 2008, 03:17 PM
  3. JDO Transactions and JUnit testing
    By markds75 in forum Data
    Replies: 2
    Last Post: Sep 17th, 2005, 01:46 AM
  4. JUnit Tests and Eclipse
    By bgsmith in forum Web
    Replies: 2
    Last Post: Mar 8th, 2005, 09:10 PM
  5. Petclinic example and junit tests
    By spring04 in forum Architecture
    Replies: 1
    Last Post: Nov 17th, 2004, 01:06 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
  •