### Question:
GenericOrderFieldReferenceValidator implements ConstraintValidator<CheckGenericOrderFieldReference, ClientGenericOrder>, ValidationErrorDescriptorBuilder { @Override public boolean isValid(ClientGenericOrder order, ConstraintValidatorContext context) { if (EMPTY.equals(order.getFieldReference())) return false; return order.getFieldReference() != null ^ order.isAggregationLevel(); }  @Override void initialize(CheckGenericOrderFieldReference checkGenericOrderFieldReference); @Override boolean isValid(ClientGenericOrder order, ConstraintValidatorContext context); @Override ErrorDescriptor build(ConstraintViolation violation);  }### Answer:
@Test public void isValid_nullOrder_throwsException() { final ConstraintValidatorContext stub = mock(ConstraintValidatorContext.class); assertThrows( NullPointerException.class, new Executable() { @Override public void execute() { objectUnderTests.isValid( null, stub ); } } ); }
@Test public void isValid_orderWithNullFieldReferenceAndAggregationLevelNull_throwsException() { final ConstraintValidatorContext stub = mock(ConstraintValidatorContext.class); final ClientGenericOrder order = new ClientGenericOrder() .setFieldReference(null) .setAggregation(null); assertThrows( NullPointerException.class, new Executable() { @Override public void execute() { objectUnderTests.isValid( order, stub ); } } ); }
@Test public void isValid_orderWithNullFieldReferenceAndAggregationLevelNotSet_false() { final ConstraintValidatorContext stub = mock(ConstraintValidatorContext.class); final ClientGenericOrder order = new ClientGenericOrder() .setFieldReference(null); boolean isValid = objectUnderTests.isValid(order, stub); assertFalse(isValid); verifyZeroInteractions(stub); }
@Test public void isValid_orderWithNullFieldReferenceAndAggregationLevelSet_true() { final ConstraintValidatorContext stub = mock(ConstraintValidatorContext.class); final ClientGenericOrder order = new ClientGenericOrder() .setFieldReference(null) .setAggregation(true); boolean isValid = objectUnderTests.isValid(order, stub); assertTrue(isValid); verifyZeroInteractions(stub); }
@Test public void isValid_orderWithSomeFieldReferenceAndAggregationLevelNotSet_true() { final ConstraintValidatorContext stub = mock(ConstraintValidatorContext.class); final ClientGenericOrder order = new ClientGenericOrder() .setFieldReference(TEST_REFERENCE); boolean isValid = objectUnderTests.isValid(order, stub); assertTrue(isValid); verifyZeroInteractions(stub); }
@Test public void isValid_orderWithSomeFieldReferenceAndAggregationLevelSet_false() { final ConstraintValidatorContext stub = mock(ConstraintValidatorContext.class); final ClientGenericOrder order = new ClientGenericOrder() .setFieldReference(TEST_REFERENCE) .setAggregation(true); boolean isValid = objectUnderTests.isValid(order, stub); assertFalse(isValid); verifyZeroInteractions(stub); }