I don't know if you managed to figure out what i am trying to do just from the title so I'll try to explain with example
Lets suppose I have created my own annotation @VerifySomething
And I have created test class for that annotation to make sure it works.
Now lets suppose that I have class SomeClass
with field something
anoted with annotation @VerifySomething
class SomeClass {
@VerifySomething
String something;
}
So far so good, but now I want to create test class
for my SomeClass
however I don't see any point in verifying all the test cases of something as I have already checked that @VerifySomething
works as it should in class which tests that annotation, however I need to make sure that field something actually has @VerifySomething
annotation without copy pasting all these test cases.
So my question is, is there a way to check if some field has one or more required annotation without writing test cases I have already written in annotation test class to make sure it works as it should.
The isAnnotation() method is used to check whether a class object is an annotation. The isAnnotation() method has no parameters and returns a boolean value. If the return value is true , then the class object is an annotation. If the return value is false , then the class object is not an annotation.
The @Field annotation is a generic form of the @Column annotation, which is not specific to relational databases. You can use @Field to map EIS and NoSQL data. Examples. See "@NoSql" for an example of the @Field annotation.
Field-level annotations for semantic types. Specifies the name of the field in the schema. Specifies the name of the property to be added at the field level. Specifies the value of the property as a string.
You can use getAnnotation
to determine if there is a specific Annotation on the field, which takes the annotation class as a parameter:
field.getAnnotation( SomeAnnotation.class );
Here is a method you can use to verify that a class has a field annotated by given annotation:
import java.lang.reflect.Field;
import javax.validation.constraints.NotNull;
import org.junit.Test;
import org.springframework.util.Assert;
public class TestHasAnnotatedField {
@Test
public void testHasFieldsWithAnnotation() throws SecurityException, NoSuchFieldException {
Class<?>[] classesToVerify = new Class[] {MyClass1.class, MyClass2.class};
for (Class<?> clazz : classesToVerify) {
Field field = clazz.getDeclaredField("something");
Assert.notNull(field.getAnnotation(NotNull.class));
}
}
static class MyClass1 { @NotNull String something; }
static class MyClass2 { @NotNull String something; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With