Reputation: 23322
I've seen lots of great articles explaining JUnit and articles explaining Annotations but nothing quite explaining how the two actually tie together.
I know that the annotation @test
is places in front of a method that you want to be used a JUnit test, but can someone please explain what's going on in the background?
How are annotations converted into useful Java code? Where does it happen? How can I make something like this myself?
Upvotes: 0
Views: 101
Reputation: 115328
JUnit was born before annotations. That time all test methods must be called with prefix test
. For example
testX()
testSomeStuff()
etc, etc.
Additionally all test cases had to be extended from abstract class TestCase
that had method that examined the actual class by reflection extracting all no-args not static methods that start with test
and running them.
Then JUnit4 arrived. The TestCase
and tests naming convention was deprecated. Instead test runner and annotations were introduced. However the idea remained. The test runner examines test case class extracting all methods annotated using @Test
annotation and runs them. Test runner is also pluggable using annotation @RunWith
that can be applied to the test case class. Default test runner is JUnit4
.
Upvotes: 2
Reputation: 77177
JUnit uses standard reflection to list the methods and see if any of them have JUnit annotations. If they do, JUnit uses reflection to invoke them.
The source code for JUnit is available, and I recommend reading the source for the test runners.
Upvotes: 0