Package io.blt.test.assertj
Class AnnotationAssertions
java.lang.Object
io.blt.test.assertj.AnnotationAssertions
Assertion factory methods that allow testing the annotations of various types.
Each method is a static factory for an annotation specific assertion object
i.e. each method returns ObjectAssert<T extends Annotation>.
@Test
void isAnnotatedAsTransactionalWithNoRollbackForException() {
assertHasAnnotation(NotificationPublisher.class, Transactional.class)
.extracting(Transactional::noRollbackFor)
.isEqualTo(Exception.class);
}
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Annotation>
org.assertj.core.api.ObjectAssert<T> assertHasAnnotation(Class<?> clazz, Class<T> annotation) Asserts that aClassis annotated with a given annotation.static <T extends Annotation>
org.assertj.core.api.ObjectAssert<T> assertHasAnnotation(Field field, Class<T> annotation) Asserts that aFieldis annotated with a given annotation.static <T extends Annotation>
org.assertj.core.api.ObjectAssert<T> assertHasAnnotation(Method method, Class<T> annotation) Asserts that aMethodis annotated with a given annotation.
-
Method Details
-
assertHasAnnotation
public static <T extends Annotation> org.assertj.core.api.ObjectAssert<T> assertHasAnnotation(Method method, Class<T> annotation) Asserts that aMethodis annotated with a given annotation.If present, an assertion object is returned for the found annotation instance, else the test fails.
e.g.@Test void isAnnotatedAsTransactionalWithNoRollbackForException() throws Exception { var method = NotificationPublisher.class.getMethod("sendNotification"); assertHasAnnotation(method, Transactional.class) .extracting(Transactional::noRollbackFor) .isEqualTo(Exception.class); }- Type Parameters:
T- type ofAnnotation- Parameters:
method- aMethodto test for the presence ofannotationannotation- the expectedAnnotationtype- Returns:
- an assertion object for the found annotation i.e.
ObjectAssert<T extends Annotation>
-
assertHasAnnotation
public static <T extends Annotation> org.assertj.core.api.ObjectAssert<T> assertHasAnnotation(Class<?> clazz, Class<T> annotation) Asserts that aClassis annotated with a given annotation.If present, an assertion object is returned for the found annotation instance, else the test fails.
e.g.@Test void isAnnotatedAsTransactionalWithNoRollbackForException() { assertHasAnnotation(NotificationPublisher.class, Transactional.class) .extracting(Transactional::noRollbackFor) .isEqualTo(Exception.class); }- Type Parameters:
T- type ofAnnotation- Parameters:
clazz- aClassto test for the presence ofannotationannotation- the expectedAnnotationtype- Returns:
- an assertion object for the found annotation i.e.
ObjectAssert<T extends Annotation>
-
assertHasAnnotation
public static <T extends Annotation> org.assertj.core.api.ObjectAssert<T> assertHasAnnotation(Field field, Class<T> annotation) Asserts that aFieldis annotated with a given annotation.If present, an assertion object is returned for the found annotation instance, else the test fails.
e.g.@Test void isAnnotatedAsDigitsWithFractionOfTwo() { var method = Invoice.class.getField("price"); assertHasAnnotation(field, Digits.class) .extracting(Digits::fraction) .isEqualTo(2); }- Type Parameters:
T- type ofAnnotation- Parameters:
field- aFieldto test for the presence ofannotationannotation- the expectedAnnotationtype- Returns:
- an assertion object for the found annotation i.e.
ObjectAssert<T extends Annotation>
-