How to use Interface TestValidator class of org.junit.validator package

Best junit code snippet using org.junit.validator.Interface TestValidator

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

No snippet found for this class 😞

copy
1@Target(ElementType.FIELD)
2@Retention(RetentionPolicy.RUNTIME)
3@Constraint(validatedBy = MyValidator.class)
4public @interface MyAnnotation {
5    String message() default "Invalid value (it must be foo)";
6    Class<?>[] groups() default {};
7    Class<? extends Payload>[] payload() default {};
8
9}
10
Full Screen
copy
1public class MyValidator implements ConstraintValidator<MyAnnotation, String> {
2    @Override
3    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
4        if (null == s) return true;
5        return "foo".equalsIgnoreCase(s);
6    }
7}
8
Full Screen
copy
1@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2public class MyValidatorTest {
3
4    private Validator validator;
5
6    @BeforeAll
7    void init() {
8        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
9        validator = factory.getValidator();
10    }
11
12    private static class TestObject {
13
14        @MyAnnotation
15        private String testField;
16
17        TestObject() {
18            this(null);
19        }
20
21        TestObject(String value) {
22            testField = value;
23        }
24
25        public String getTestField() {
26            return testField;
27        }
28
29        public void setTestField(String testField) {
30            this.testField = testField;
31        }
32    }
33
34    @Test
35    void shouldValidForNullValue() {
36        var obj = new TestObject();
37        var violations = validator.validate(obj); // Set<ConstraintViolation<TestObject>>
38        Assertions.assertTrue(violations.isEmpty(), String.format("Object should valid, but has %d violations", violations.size()));
39    }
40
41    @Test
42    void shouldValidForFooValue() {
43        var obj = new TestObject("foo");
44        var violations = validator.validate(obj); // Set<ConstraintViolation<TestObject>>
45        Assertions.assertTrue(violations.isEmpty(), String.format("Object should valid, but has %d violations", violations.size()));
46    }
47
48    @Test
49    void shouldInvalidForBarValue() {
50        var obj = new TestObject("bar");
51        var violations = validator.validate(obj); // Set<ConstraintViolation<TestObject>>
52        Assertions.assertEquals(1, violations.size());
53    }
54}
55
Full Screen
copy
1@Target(ElementType.FIELD)
2@Retention(RetentionPolicy.RUNTIME)
3@Constraint(validatedBy = MyHasAttributesValidator.class)
4public @interface MyAnnotationHasAttributes {
5    String message() default "Invalid value (it must be foo)";
6    Class<?>[] groups() default {};
7    Class<? extends Payload>[] payload() default {};
8
9    int attributeOne() default 10;
10    int attributeTwo() default 20;
11
12}
13
Full Screen
copy
1public class MyHasAttributesValidator implements ConstraintValidator<MyAnnotationHasAttributes, String> {
2    private MyAnnotationHasAttributes ann;
3
4    @Override
5    public void initialize(MyAnnotationHasAttributes constraintAnnotation) {
6        ann = constraintAnnotation;
7    }
8
9    @Override
10    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
11        if (null == s) return true;
12        return s.length() >= ann.attributeOne() && s.length() < ann.attributeTwo();
13    }
14}
15
Full Screen
copy
1public class HasAttributeValidatorTest {
2
3    private MyAnnotationHasAttributes createAnnotation(Integer one, Integer two) {
4        final Map<String, Object> attrs = new HashMap<>();
5        if (null != one) {
6            attrs.put("attributeOne", one);
7        }
8        if (null != two) {
9            attrs.put("attributeOne", two);
10        }
11        var desc = new AnnotationDescriptor.Builder<>(MyAnnotationHasAttributes.class, attrs).build();
12        return AnnotationFactory.create(desc);
13    }
14
15    @ParameterizedTest
16    @MethodSource("provideValues")
17    void testValidator(Integer one, Integer two, String input, boolean expected) {
18        MyAnnotationHasAttributes ann = createAnnotation(one, two);
19        MyHasAttributesValidator validator = new MyHasAttributesValidator();
20        validator.initialize(ann);
21        var result = validator.isValid(input, null);
22        Assertions.assertEquals(expected, result, String.format("Validation must be %s but found: %s with params: %d, %d, %s", expected, result, one, two, input));
23    }
24
25    private static Stream<Arguments> provideValues() {
26        return Stream.of(
27                Arguments.of(null, null, null, true),
28                Arguments.of(null, 20, "foo", true),
29                Arguments.of(null, null, RandomStringUtils.randomAlphabetic(30), false)
30        );
31    }
32}
33
Full Screen
copy
1@Override
2public ProductGetResponse getProductBulk(@TestValidator ProductGetKey productGetKey) throws ItemWrapperApiException
3
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in Interface-TestValidator

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)