title: Test Custom Validator with Autowired spring Service
body: <p>I have a custom Hibernate Validator for my entities. One of my validators uses an Autowired Spring @Repository. The application works fine and my repository is Autowired successfully on my validator.</p>
  
  <p>The problem is i can't find a way to test my validator, cause i can't inject my repository inside it.</p>
  
  <p>Person.class:</p>
  
  <pre><code>@Entity
  @Table(schema = "dbo", name = "Person")
  @PersonNameMustBeUnique
  public class Person {
  
      @Id
      @GeneratedValue
      @Column(name = "id", unique = true, nullable = false)
      private Integer id;
  
      @Column()
      @NotBlank()
      private String name;
  
      //getters and setters
      //...
  }
  </code></pre>
  
  <p>PersonNameMustBeUnique.class</p>
  
  <pre><code>@Target({ TYPE, ANNOTATION_TYPE })
  @Retention(RUNTIME)
  @Constraint(validatedBy = { PersonNameMustBeUniqueValidator.class })
  @Documented
  public @interface PersonNameMustBeUnique{
      String message() default "";
  
      Class&lt;?&gt;[] groups() default {};
  
      Class&lt;? extends javax.validation.Payload&gt;[] payload() default {};
  }
  </code></pre>
  
  <p>The validator:</p>
  
  <pre><code>public class PersonNameMustBeUniqueValidatorimplements ConstraintValidator&lt;PersonNameMustBeUnique, Person&gt; {
  
      @Autowired
      private PersonRepository repository;
  
      @Override
      public void initialize(PersonNameMustBeUnique constraintAnnotation) { }
  
      @Override
      public boolean isValid(Person entidade, ConstraintValidatorContext context) {
          if ( entidade == null ) {
              return true;
          }
  
          context.disableDefaultConstraintViolation();
  
          boolean isValid = nameMustBeUnique(entidade, context);
  
          return isValid;
      }
  
      private boolean nameMustBeUnique(Person entidade, ConstraintValidatorContext context) {
          //CALL REPOSITORY TO CHECK IF THE NAME IS UNIQUE 
          //ADD errors if not unique...
      }
  }
  </code></pre>
  
  <p>And the context file has a validator bean:</p>
  
  <pre><code>&lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/&gt;
  </code></pre>
  
  <p>Again, it works fine, but i don't know how to test it.</p>
  
  <p>My test file is:</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class PersonTest {
  
      Person e;
      static Validator validator;
  
      @BeforeClass
      public static void setUpClass() {
          ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
          validator = factory.getValidator();
      }
  
      @Test
      public void name__must_not_be_null() {
          e = new Person();
          e.setName(null);
          Set&lt;ConstraintViolation&lt;Person&gt;&gt; violations = validator.validate(e);
          assertViolacao(violations, "name", "Name must not be null");
      }
  
  }
  </code></pre>