title: Mock a ConstraintValidator of a @Validated annotated controller method on Spring
body: <p>Using Spring Boot 1.3.6.RELEASE, i am trying to unit test a controller method using Junit, Mockito and MockMvc. I have built a custom constraint validator (extending <a href="https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html" rel="nofollow">ConstraintValidator</a>) which autowires a service. My target entity is annotated with the custom validator annotation and a group. My <strong>controller method signature</strong> is the following :
  <pre><code>@RequestMapping ( value = "api/task/newtask", method = RequestMethod.POST )
  public ResponseEntity submitTask ( <strong>@Validated ( value = TaskDependenciesDbValidation.class )</strong> 
                                     @RequestBody Task task )</pre></code>
  isValid(..) method of my custom validator</p>
  
  <p><pre><code>@Override
      public boolean <strong>isValid</strong> ( Ticket ticket, ConstraintValidatorContext context )
      {
          try
          {
              this.ticketService.verifyTicketExists( ticket.getId() );
              return true;
          } catch ( ResourceNotFoundException e )
          {
              context.disableDefaultConstraintViolation();
              context.buildConstraintViolationWithTemplate( "Ticket with id " + ticket.getId() + " not found" )
                      .addConstraintViolation();
              return false;
          }
      }</pre></code></p>
  
  <p>On runtime, everything works fine.</p>
  
  <p>I would like to unit test my controller's submitTask method by mocking my custom constraint validator entirely or just mock the <strong>ticketService</strong> that the validator uses.</p>
  
  <p>I have tried mocking the service and the validator "traditionally" (not at the same time) with Mockito.when(..) but i am getting a NullPointerException on the service.</p>
  
  <h2>Edit :</h2>
  
  <p>Test attempt :</p>
  
  <pre><code>@RunWith ( SpringJUnit4ClassRunner.class )
  @SpringApplicationConfiguration ( classes = MyApplication.class )
  @ContextConfiguration ( classes = MockServletContext.class )
  @WebAppConfiguration
  public class TaskControllerTests
  {
      @InjectMocks
      TaskController taskController;
      @Mock
      TaskService taskService;
      @Mock
      TicketService ticketService;
      .
      .
      @Before
      public void setup ()
      {
          MockitoAnnotations.initMocks( this );
          mockMvc = standaloneSetup( this.taskController )
              .setControllerAdvice( new RestExceptionHandler() )
              .build();
      }
      .
      .
      @Test
      public void submitTask () throws Exception
      {
          when( taskService.create( any( Task.class ) ) ).thenReturn( this.task );
          doNothing().when( ticketService ).verifyTicketExists( 1L );
          mockMvc.perform( post( "/api/task/newtask" ).content( "..validpayload..") ).andExpect( status().isCreated() );
      }
  }
  </code></pre>