title: Testing validation constraints
body: <p>The Hibernate Validator documentation has a simple Getting Started guide which outlines <a href="http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html/validator-gettingstarted.html#d0e168" rel="nofollow">testing validation rules</a>.</p>
  
  <p>The relevant chunk is</p>
  
  <pre><code>@Test
  public void manufacturerIsNull() {
      Car car = new Car(null, "DD-AB-123", 4);
  
      Set&lt;ConstraintViolation&lt;Car&gt;&gt; constraintViolations =
          validator.validate(car);
  
      assertEquals(1, constraintViolations.size());
      assertEquals("may not be null", constraintViolations.iterator().next().getMessage());
  }
  </code></pre>
  
  <p>It seems to me a fairly vague route to test if the NotNull constraint has been violated.</p>
  
  <p>My simple solution would be something like</p>
  
  <pre><code>public static &lt;T&gt; boolean  containsConstraintViolation(
      Set&lt;ConstraintViolation&lt;T&gt;&gt; violations, Class&lt;?&gt; constraint) {
  
      for (ConstraintViolation&lt;?&gt; violation : violations) {
          ConstraintDescriptor&lt;?&gt; descriptor = violation.getConstraintDescriptor();
              if (constraint.isAssignableFrom(descriptor.getAnnotation().getClass()))
                  return true;
          }
      return false;
  }
  </code></pre>
  
  <p>Which allows me to do tests like</p>
  
  <pre><code>assertTrue(ValidationUtils.containsConstraintViolation(violations, NotNull.class));
  </code></pre>
  
  <p>However, I'm sure this is going to be naive in the long run and am wondering if there's not some other library or API I'm missing to assist in unit testing constraints.</p>