### Question:
ValidationGroupHelper { public static boolean hasMatchingValidationGroup(String targetGroups, AnnotationMirror annotationMirror) { List<String> activeValidationGroups = ValidationGroupHelper.getActiveValidationGroups(targetGroups); if (annotationMirror != null) { List<String> groupsOnField = getGroupsOnField(annotationMirror.getElementValues().toString()); if (runningWithDefaultGroupOnFieldInDefaultGroup(groupsOnField, activeValidationGroups)) { return true; } if (validationGroupOnFieldMatchesWithActiveGroup(groupsOnField, activeValidationGroups)) { return true; } } return false; }  static boolean hasMatchingValidationGroup(String targetGroups, AnnotationMirror annotationMirror);  }### Answer:
@Test public void verifyNotRequiredWhenConfigHasGroupAndFieldHasMatchingGroups() { AnnotationMirror mockedMirror = Mockito.mock(AnnotationMirror.class, Answers.RETURNS_DEEP_STUBS); Mockito.when(mockedMirror.getElementValues().toString()).thenReturn("{names()={a,b},groups()={com.a.A.class, com.b.B.class},age()={12}}"); assertTrue("Field does have @NotNull with groups, and validation runs with specific group", ValidationGroupHelper.hasMatchingValidationGroup("com.c.C, com.b.B", mockedMirror)); }
@Test public void verifyNotRequiredWhenConfigHasGroupAndFieldHasNoMatchingGroups() { AnnotationMirror mockedMirror = Mockito.mock(AnnotationMirror.class, Answers.RETURNS_DEEP_STUBS); Mockito.when(mockedMirror.getElementValues().toString()).thenReturn("{names()={a,b},groups()={com.a.A.class, com.d.D.class},age()={12}}"); assertFalse("Field does have @NotNull with groups, and validation runs with specific group, but none is matching", ValidationGroupHelper.hasMatchingValidationGroup("com.c.C, com.b.B", mockedMirror)); }
@Test public void verifyNotRequiredWhenNotNullAnnotationIsMissing() { assertFalse("Field does not have @NotNull", ValidationGroupHelper.hasMatchingValidationGroup("com.a.A, com.b.B", null)); }
@Test public void verifyNotRequiredWhenConfigHasDefaultGroupAndFieldHasGroups() { AnnotationMirror mockedMirror = Mockito.mock(AnnotationMirror.class, Answers.RETURNS_DEEP_STUBS); Mockito.when(mockedMirror.getElementValues().toString()).thenReturn("{names()={a,b},groups()={com.a.A.class, com.b.B.class},age()={12}}"); assertFalse("Field does not have @NotNull with groups, but validation runs with default group", ValidationGroupHelper.hasMatchingValidationGroup("", mockedMirror)); }
@Test public void verifyNotRequiredWhenConfigHasDefaultGroupAndFieldHasNoGroups() { AnnotationMirror mockedMirror = Mockito.mock(AnnotationMirror.class, Answers.RETURNS_DEEP_STUBS); Mockito.when(mockedMirror.getElementValues().toString()).thenReturn("{names()={a,b},age()={12}}"); assertTrue("Field does have @NotNull without groups and validation runs with default group", ValidationGroupHelper.hasMatchingValidationGroup("", mockedMirror)); }
@Test public void verifyNotRequiredWhenConfigHasGroupAndFieldHasNoGroups() { AnnotationMirror mockedMirror = Mockito.mock(AnnotationMirror.class, Answers.RETURNS_DEEP_STUBS); Mockito.when(mockedMirror.getElementValues().toString()).thenReturn("{names()={a,b},age()={12}}"); assertFalse("Field does have @NotNull without groups, but validation runs with specific groups", ValidationGroupHelper.hasMatchingValidationGroup("com.c.C, com.b.B", mockedMirror)); }