How to test that Annotation @ApiModelProprty is present on all fields of a class?

+1 vote
14 views
asked Nov 21 by Rahul Singh (644 points)  

I am creating swagger documentation for a Rest API. I want to write a test so that I can make sure that all the fields of the Model are annotated with @ApiModelProperty.

@ApiModel
public class Model {

  @ApiModelProperty(required = true)
  @NotBlank String name;

   @ApiModelProperty(required = true)
  @Positive Integer age;
}

What is the proper way to achieve this?

2 Answers

+1 vote
answered Nov 21 by Hitesh Garg (434 points)  
selected Nov 21 by Rahul Singh
 
Best answer

I have tried this once and then used the reflection to test whether some annotations are present or not on the fields.

There are possibilities to use some other Utils to get the classes and fields but in this example we are using pure java functionalities.

This test will also test all the fields of even all the nested classes of the Model class.

class ModelTest {
  private final Class<?> modelUnderTest = Model.class;
  private final List<Class<?>> allClasses = new ArrayList<>();

  private static boolean isStatic(int modifiers) {
    return Modifier.isStatic(modifiers);
  }

  private static boolean isNotStaticClass(Class<?> clazz) {
    return !isStatic(clazz.getModifiers());
  }

  @BeforeEach
  void setUp() {
    allClasses.add(modelUnderTest);
    Arrays.stream(modelUnderTest.getDeclaredClasses())
        // Skipping with Builder as in reality model was using the @lombok.Builder annotation 
        .filter(c -> !c.getName().contains("Builder"))
        .forEach(allClasses::add);
  }

  @Test
  void shouldBeAnnotatedWithApiModel() {
    Assert.assertTrue(modelUnderTest.isAnnotationPresent(ApiModel.class));
  }

  @Test
  void shouldHaveAllFieldsMarkedWithApiModelProperty() {
    forAllFields(
        field -> {
          if (!field.isAnnotationPresent(ApiModelProperty.class)) {
            Assert.fail("Annotation ApiModelPropertyAnnotation missing on " + field);
          } else {
            var annotation = field.getAnnotation(ApiModelProperty.class);
            assertThat(annotation.required()).isTrue();
          }
        });
  }

  @Test
  void stringsFieldsShouldBeMarkedWithNotBlank() {
    forAllFields(
        field -> {
          if (field.getType().equals(String.class)
              && !field.isAnnotationPresent(NotBlank.class)) {
            Assert.fail("Annotation NotBlank missing on " + field);
          }
        });
  }

  @Test
  void numbersFieldsShouldBeMarkedWithPositive() {
    forAllFields(
        field -> {
          if (field.getType().isAssignableFrom(Number.class)
              && !field.isAnnotationPresent(Positive.class)) {
            Assert.fail("Annotation Positive missing on " + field);
          }
        });
  }

  private void forAllFields(Consumer<Field> consumer) {
    for (var toTest : allClasses) {
      for (Field field : toTest.getDeclaredFields()) {
        if (isStatic(field.getModifiers())) {
          continue;
        }
        consumer.accept(field);
      }
    }
  }
}
0 votes
answered 1 day ago by Onlinefront (10 points)  

Web Development Packages in Delhi | Website Designing & Development Services | Online Front

Online Front website design & development services combine advanced web design technology with the latest business concepts to make your website attractive and easy to understand. Online Front provides the best budget-friendly Web development packages in Delhi

Our Services

Web Devlopment Packages in Delhi

Digital Marketing Company

Website Design package

SEO Services package

YouTube Marketing

Website Design

...