### Question:
CoconutValidator { public static boolean areFieldsValid(CoconutView... views) { boolean ret = true; for (CoconutView view : views) { CoconutInput inputView = view.getCoconutView(); if (inputView == null || inputView.isOptional()) { continue; } String input = inputView.getInput(); if (!inputView.isOptional() && input == null) { ret = false; continue; } String regex = inputView.getValidationRegex(); String errorMessage = inputView.getErrorMessages(); if (regex != null && errorMessage != null && input != null) { boolean patternMatches = Pattern.compile(regex).matcher(input) .matches(); if (!patternMatches) { ret = false; view.setErrorMessage(errorMessage); } } } return ret; } private  CoconutValidator(); static boolean areFieldsValid(CoconutView... views); @Deprecated static boolean areFieldsValidRecursive(ViewGroup parent); static boolean validateLayout(ViewGroup parent); @Deprecated static boolean validateLayout(View parent);  }### Answer:
@Test public void testTrueForNullInputView() { boolean condition = CoconutValidator.areFieldsValid(coconutView); Assert.assertTrue(condition); }
@Test public void testTrueForAllOptionalFields() { when(coconutView.getCoconutView()).thenReturn(coconutInput); when(coconutInput.isOptional()).thenReturn(true); boolean condition = CoconutValidator.areFieldsValid(coconutView); Assert.assertTrue(condition); }
@Test public void testFalseForNonOptionalNullInput() { when(coconutView.getCoconutView()).thenReturn(coconutInput); boolean condition = CoconutValidator.areFieldsValid(coconutView); Assert.assertFalse(condition); }
@Test public void testTrueForNonOptionalNullRegexAndNullErrorMessage() { when(coconutView.getCoconutView()).thenReturn(coconutInput); when(coconutInput.getInput()).thenReturn("foo"); boolean condition = CoconutValidator.areFieldsValid(coconutView); Assert.assertTrue(condition); }
@Test public void testTrueForNonOptionalNullRegex() { when(coconutView.getCoconutView()).thenReturn(coconutInput); when(coconutInput.getInput()).thenReturn("foo"); when(coconutInput.getErrorMessages()).thenReturn("bar"); boolean condition = CoconutValidator.areFieldsValid(coconutView); Assert.assertTrue(condition); }
@Test public void testTrueForNonOptionalNullErrorMessage() { when(coconutView.getCoconutView()).thenReturn(coconutInput); when(coconutInput.getInput()).thenReturn("foo"); when(coconutInput.getValidationRegex()).thenReturn("bar"); boolean condition = CoconutValidator.areFieldsValid(coconutView); Assert.assertTrue(condition); }
@Test public void testTrueForMatchedPattern() { when(coconutView.getCoconutView()).thenReturn(coconutInput); when(coconutInput.getInput()).thenReturn("1234"); when(coconutInput.getValidationRegex()).thenReturn("[0-9]+"); when(coconutInput.getErrorMessages()).thenReturn("only numbers"); boolean condition = CoconutValidator.areFieldsValid(coconutView); Assert.assertTrue(condition); }
@Test public void testFalseForInvalidMatch() { when(coconutView.getCoconutView()).thenReturn(coconutInput); when(coconutInput.getInput()).thenReturn("string"); when(coconutInput.getValidationRegex()).thenReturn("[0-9]+"); when(coconutInput.getErrorMessages()).thenReturn("only numbers"); boolean condition = CoconutValidator.areFieldsValid(coconutView); Assert.assertFalse(condition); }
@Test public void testErrorMessageIsSetForInvalidMatch() { String errorMessage = "only numbers"; when(coconutView.getCoconutView()).thenReturn(coconutInput); when(coconutInput.getInput()).thenReturn("invalid"); when(coconutInput.getValidationRegex()).thenReturn("[0-9]+"); when(coconutInput.getErrorMessages()).thenReturn(errorMessage); boolean condition = CoconutValidator.areFieldsValid(coconutView); Mockito.verify(coconutView).setErrorMessage(errorMessage); Assert.assertFalse(condition); }