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 aClass
is annotated with a given annotation.static <T extends Annotation>
org.assertj.core.api.ObjectAssert<T> assertHasAnnotation
(Field field, Class<T> annotation) Asserts that aField
is annotated with a given annotation.static <T extends Annotation>
org.assertj.core.api.ObjectAssert<T> assertHasAnnotation
(Method method, Class<T> annotation) Asserts that aMethod
is 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 aMethod
is 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
- aMethod
to test for the presence ofannotation
annotation
- the expectedAnnotation
type- 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 aClass
is 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
- aClass
to test for the presence ofannotation
annotation
- the expectedAnnotation
type- 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 aField
is 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
- aField
to test for the presence ofannotation
annotation
- the expectedAnnotation
type- Returns:
- an assertion object for the found annotation i.e.
ObjectAssert<T extends Annotation>
-