@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); }
@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; } }
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; } } }
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; } }  }
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); }
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);  }