### Question:
Check { public static void matchesPattern(final String field, final String value, final Pattern pattern) throws ValidationException { if (value == null || !pattern.matcher(value).matches()) { final String errorMessage = String.format( DOES_NOT_MATCH_PATTERN + ", value -> [%s], expected pattern -> [%s]", value, pattern.toString()); throw new ValidationException(errorMessage, field); } }  static void isNotEmptyOrNull(final String field, final String value); static void isNotEmptyOrNull(final String field, final Collection<?> value); static void isNotNull(final String field, final Object value); static void isValidEmail(final String field, final String email); static void isValidPhoneNumber(final String field, final String phoneNumber); static void contains(final String field, final String value, final CharSequence expectedSubsequence); static void isLength(final String field, final String string, final int expectedLength); static void isBetween(final String field, final int value, final int minValue, final int maxValue); static void matchesPattern(final String field, final String value, final Pattern pattern);  }### Answer:
@Test public void shouldNotThrowValidationException_withStringValueThatMatchesPattern() { final String value = Randoms.randomString(); final Pattern pattern = Pattern.compile(Pattern.quote(value)); ValidationException actualException = null; try { Check.matchesPattern(field, value, pattern); } catch (final ValidationException e) { actualException = e; } assertNull(actualException); }
@Test public void shouldThrowValidationException_withNullStringValueForPatternMatch() { final Pattern pattern = Pattern.compile(Pattern.quote(Randoms.randomString())); final String value = null; ValidationException actualException = null; try { Check.matchesPattern(field, value, pattern); } catch (final ValidationException e) { actualException = e; } assertNotNull(actualException); assertThat(actualException.getFields()[0], is(field)); }
@Test public void shouldThrowValidationException_withStringValueThatDoesNotMatchPattern() { final String value = Randoms.randomString(); final Pattern pattern = Pattern.compile("EXTRA" + Pattern.quote(value)); ValidationException actualException = null; try { Check.matchesPattern(field, value, pattern); } catch (final ValidationException e) { actualException = e; } assertNotNull(actualException); assertThat(actualException.getFields()[0], is(field)); }