Search in sources :

Example 1 with Payload

use of jakarta.validation.Payload in project hibernate-validator by hibernate.

the class GetAnnotationsParameterTest method testGetMessageParameter.

@Test
public void testGetMessageParameter() {
    NotNull testAnnotation = new NotNull() {

        @Override
        public String message() {
            return "test";
        }

        @Override
        public Class<?>[] groups() {
            return new Class<?>[] { Default.class };
        }

        @Override
        public Class<? extends Payload>[] payload() {
            @SuppressWarnings("unchecked") Class<? extends Payload>[] classes = new Class[] {};
            return classes;
        }

        @Override
        public Class<? extends Annotation> annotationType() {
            return this.getClass();
        }
    };
    String message = GetAnnotationAttribute.action(testAnnotation, "message", String.class).run();
    assertEquals("test", message, "Wrong message");
    Class<?>[] group = GetAnnotationAttribute.action(testAnnotation, "groups", Class[].class).run();
    assertEquals(group[0], Default.class, "Wrong group");
    try {
        GetAnnotationAttribute.action(testAnnotation, "message", Integer.class).run();
        fail();
    } catch (ValidationException e) {
        assertThat(e.getMessage()).startsWith("HV000082").as("Wrong exception message");
    }
    try {
        GetAnnotationAttribute.action(testAnnotation, "foo", Integer.class).run();
        fail();
    } catch (ValidationException e) {
        assertThat(e.getMessage()).startsWith("HV000083").as("Wrong exception message");
    }
}
Also used : ValidationException(jakarta.validation.ValidationException) Payload(jakarta.validation.Payload) NotNull(jakarta.validation.constraints.NotNull) Default(jakarta.validation.groups.Default) Test(org.testng.annotations.Test)

Aggregations

Payload (jakarta.validation.Payload)1 ValidationException (jakarta.validation.ValidationException)1 NotNull (jakarta.validation.constraints.NotNull)1 Default (jakarta.validation.groups.Default)1 Test (org.testng.annotations.Test)1