Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Java annotations be unit tested?

I've recently started creating my own annotations and to sport TDD/BDD, I'd want to unit test my annotations to create a clear specification for them. However since annotations are basically merely fancy interfaces which to my knowledge can't be really instantiated directly, is there any way short of reflection to unit test an annotation?

like image 882
Esko Avatar asked Mar 24 '09 09:03

Esko


People also ask

Which annotation is used for unit testing?

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.

What are JUnit test annotations?

org.junit The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure.

Does Java do unit testing?

Unit Testing is a methodology of testing source code for its fitment of use in production. We start out writing unit tests by creating various test cases to verify the behaviors of an individual unit of source code.

Which annotation we can use to manage JUnit test?

JUnit provides an annotation called @Test, which tells the JUnit that the public void method in which it is used can run as a test case. To execute multiple tests in a specified order, it can be done by combining all the tests in one place. This place is called as the test suites.


1 Answers

It's not something I would usually write tests for, but you could simply create a set of test classes which use and abuse the annotation, to test that it is storing its member values, that it has the correct defaults etc.

This will only work on Runtime annotations that are specfied on the correct targets of course.

In my experience, annotations themselves are rarely interesting enough to warrant unit tests - it is usually the code which uses them that needs testing. But then I'm not from the 100% code coverage school of thought :-)

like image 85
Anthony Roy Avatar answered Sep 20 '22 09:09

Anthony Roy