/** * Validate {@code candidate} based on the regex pattern supplied. */ @Override public void validate(Metadata<Valid> metadata, Object candidate) throws AssertionError { Valid valid = metadata.getAnnotation(); String regex = valid.value(); String input = String.valueOf(candidate); if (!input.matches(regex)) throw new AssertionError(errorMessage(input,regex)); }
@Theory @Test public void When_Not_Pass_Validation_Then_Throw_Assertion_Error(Data data) throws NoSuchMethodException { String methodName = data.methodName; Method method = Providers.class.getDeclaredMethod(methodName, data.classParam); Valid annotation = (Valid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Valid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); for (Object illegal : data.illegal) { try { validMockery.validate(metadata, illegal); fail(); } catch (AssertionError e) { String expectedMessage = validMockery.errorMessage(String.valueOf(illegal), metadata.getAnnotation().value()); assertThat(expectedMessage, is(e.getMessage())); } } }