### Question:
Check { public static void isValidEmail(final String field, final String email) throws ValidationException { if (email == null || !EMAIL_ADDRESS_PATTERN.matcher(email).matches()) { final String errorMessage = String.format(INVALID_EMAIL_ADDRESS + " : value -> [%s]", email); 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 shouldThrowValidationException_givenNullEmail() { final String email = null; ValidationException actualException = null; try { Check.isValidEmail(field, email); } catch (final ValidationException e) { actualException = e; } assertNotNull(actualException); assertThat(actualException.getFields()[0], is(field)); }
@Test public void shouldThrowValidationException_givenEmptyEmail() { final String email = ""; ValidationException actualException = null; try { Check.isValidEmail(field, email); } catch (final ValidationException e) { actualException = e; } assertNotNull(actualException); assertThat(actualException.getFields()[0], is(field)); }
@Test public void shouldValidateEmailAddress_withValidEmailAddresses() { final List<String> validEmails = new ArrayList<>(); validEmails.add("prettyandsimple@example.com"); validEmails.add("very.common@example.com"); validEmails.add("disposable.style.email.with+symbol@example.com"); validEmails.add("other.email-with-dash@example.com"); validEmails.add("x@example.com"); validEmails.add("example-indeed@strange-example.com"); validEmails.add("#!$%&'*+-/=?^_`{}|~@example.org"); validEmails.add("example@s.solutions"); validEmails.add("Example@s.solutions"); final List<String> invalidatedEmails = new ArrayList<>(); for (final String email : validEmails) { try { Check.isValidEmail(field, email); } catch (final ValidationException e) { invalidatedEmails.add(email); } } if (!invalidatedEmails.isEmpty()) { fail("Expected to validate the following email addresses, but were actually invalidated: " + invalidatedEmails); } }
@Test public void shouldNotValidateEmailAddress_withInvalidEmailAddresses() { final List<String> invalidEmails = new ArrayList<>(); invalidEmails.add("Abc.example.com"); invalidEmails.add("A@b@c@example.com"); invalidEmails.add("a\"b(c)d,e:f;g<h>i[j\\k]l@example.com"); invalidEmails.add("just\"not\"right@example.com"); invalidEmails.add("this is\"not\\allowed@example.com"); invalidEmails.add("this\\ still\\\"not\\allowed@example.com"); invalidEmails.add("john.doe@example..com"); invalidEmails.add("Modapat@1"); final List<String> validatedEmails = new ArrayList<>(); for (final String email : invalidEmails) { try { Check.isValidEmail(field, email); validatedEmails.add(email); } catch (final ValidationException e) { } } if (!validatedEmails.isEmpty()) { fail("Expected to invalidate the following email addresses, but were actually validated: " + validatedEmails); } }