Announcement Announcement Module
Collapse
No announcement yet.
HibernateValidator Test Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HibernateValidator Test

    Hello,
    I am trying to test HibernateValidator, I have a class with (SportFacility) with an embedded object Address declared like this:
    Code:
    	
    @Valid
    @Embedded
    private Address address;
    Then I have my Junit:

    Code:
    	  SportFacility sf = new SportFacility();
          Set<ConstraintViolation<SportFacility>> constraintViolations = validator.validate(sf);
          if(constraintViolations.size() > 0)
          {
             Iterator<ConstraintViolation<SportFacility>> iterator = constraintViolations.iterator();
             while(iterator.hasNext())
             {
                ConstraintViolation<SportFacility> cv = iterator.next();
                System.out.println(cv.getMessage());
                System.out.println(cv.getPropertyPath());
             }
          }
    	  try
    	  {
    			sportFacilityDao.create(sf);
    	  }
          catch (org.springframework.dao.DataIntegrityViolationException  e)
    	  {
    			Assert.assertTrue(true);
    	  }
    	  catch (ConstraintViolationException e) {
    		Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator();
    		while(it.hasNext())
    		{	
    		    ConstraintViolation<?> cv = it.next();
    			System.out.println(cv.getMessage());
    			System.out.println(cv.getPropertyPath());
    		}
    	  }
    At this point I have two questions, first of all I do not get the constrains from the embedded object unless I set an empty address like this:

    Code:
          sf.setAddress(new Address());
    Is that the expected behaviour? Do I have to set explitly an empty address to get a constraint validation. Also I do not get the message, for some reason I get the {campo_obligatorio} which is the key.

    The second question is why canīt I get catch the ConstraintViolationException ? when I debug it does not enter the catch but on a normal run it does print on console the messages.....why??
Working...
X