Class AnnotationAssertions

java.lang.Object
io.blt.test.assertj.AnnotationAssertions

public final class AnnotationAssertions extends Object
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>.

e.g.
@Test
void isAnnotatedAsTransactionalWithNoRollbackForException() {
    assertHasAnnotation(NotificationPublisher.class, Transactional.class)
            .extracting(Transactional::noRollbackFor)
            .isEqualTo(Exception.class);
}
  • Method Details

    • assertHasAnnotation

      public static <T extends Annotation> org.assertj.core.api.ObjectAssert<T> assertHasAnnotation(Method method, Class<T> annotation)
      Asserts that a Method 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 of Annotation
      Parameters:
      method - a Method to test for the presence of annotation
      annotation - the expected Annotation 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 a Class 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 of Annotation
      Parameters:
      clazz - a Class to test for the presence of annotation
      annotation - the expected Annotation 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 a Field 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 of Annotation
      Parameters:
      field - a Field to test for the presence of annotation
      annotation - the expected Annotation type
      Returns:
      an assertion object for the found annotation i.e. ObjectAssert<T extends Annotation>