title: JSR 303 bean validation unit testing with Mockito and Autowiring
body: <p>I want to junit test my validator class but my validator class has @autowired service classes. How do I inject these dependencies using Mocikto? </p>
  
  <p>I am going to call the validator using below line of code.</p>
  
  <pre><code>Set&lt;ConstraintViolation&lt;MyDomainPOJOObject&gt;&gt; constraintViolationsFromJavaRules = validator.validate(myDomainPOJOObject, Default.class);
  </code></pre>
  
  <p>Problem is I am not instantiating validator class myself. It's the JSR 303 framework which truely calls the validator's isValid method(). </p>
  
  <p>Another thing is I don't want to use spring Autowiring and use the <code>@Mock and @InjectMock</code> annotations.</p>
  
  <p>Any examples or ideas?</p>
  
  <p>I was able to get it working by having below line of code</p>
  
  <pre><code>@Override
  public &lt;T extends ConstraintValidator&lt;?, ?&gt;&gt; T getInstance(Class&lt;T&gt; key) {
      if (key == com.nitin.validation.UpperCaseValidator.class) {
          return (T)upperCasevalidator;
      }
      //throw new IllegalArgumentException("expecting SomeValidationValidator!");
      return new ConstraintValidatorFactoryImpl().getInstance(key);
  }
  </code></pre>