(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?
A JUnit test is a method contained in a class which is only used for testing. This is called a Test class. To define that a certain method is a test method, annotate it with the @Test annotation. This method executes the code under test.
In any automation script, @Test annotation is the important part, where we write code/business logic. If something needs to be automated, that particular code needs to be inserted into the test method. The test method then executes @Test by passing attributes.
The @SpringBootTest annotation can be used to run quick unit tests in Spring Boot.
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.
I would't say so but it's only my personal opinion. Presence of particular annotations and their correct usage can be tested within integration tests easily. Personally, I follow rule that within unit tests I cover only java code of a particular class. DB mappings, JSR annotations, GWT UiFields markers and so on are tested within integration tests.
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