(this post explains WHY I would want to do this: Good patterns for unit testing form beans that have annotation-based validation in Spring MVC)
Is it considered good practice to write a unit test to just test configuration/annotations on a field or a class? For example if you have:
@AnnotatedClass
public class MyClass {
@AnnotatedField1
@AnnotatedField2
private string myField;
}
Is there any point in writing a unit test that checks the presence of annotations above?
The basic answer is “Yes – it’s fine to check for annotations in unit tests”
Always ask yourself “what am I testing”. Unit tests test that the “code is correct” at a class/method level.
You are not really “testing” the code, but you are asserting that coders have correctly annotated certain fields, which in your case is part of asserting that “code is correct”, so “yes” – I think it’s acceptable to “test” this.
Another option is to make this assertion part of the code style checking phase of your build (if you’re doing that) – you would have to write a custom code style to do it, but I feel that would be a more appropriate place to check this. However, that’s probably a bit painful to set up, so just do it as a unit test.