### Question:
UrlValidator implements ConstraintValidator<ValidUrl, String> { @Override public boolean isValid(String value, ConstraintValidatorContext context) { try { new URL(value); return true; } catch (MalformedURLException e) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(String.format("Invalid URL: %s", e.getMessage())) .addConstraintViolation(); return false; } }  @Override boolean isValid(String value, ConstraintValidatorContext context);  }### Answer:
@Test public void valid() { final ConstraintValidatorContext context = mock(ConstraintValidatorContext.class); final boolean result = urlValidator.isValid("http: assertThat(result).isTrue(); verifyNoMoreInteractions(context); }
@Test public void invalid() { final ConstraintValidatorContext context = mock(ConstraintValidatorContext.class); final ConstraintValidatorContext.ConstraintViolationBuilder builder = mock(ConstraintValidatorContext.ConstraintViolationBuilder.class); when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder); final boolean result = urlValidator.isValid("invalidurl", context); assertThat(result).isFalse(); verify(context).disableDefaultConstraintViolation(); verify(context).buildConstraintViolationWithTemplate("Invalid URL: no protocol: invalidurl"); verifyNoMoreInteractions(context); }