Recently learning Spring, I found that spring is using annotations (annotation), very curious about what the principle is! So it was a little bit of research on the underlying implementation principle, and then suddenly found that JUnit is using annotations, so decided to customize a simple version of the JUnit framework. The following is an analysis of the implementation principle of JUnit:
First of all we need to first declare junit annotations
I've chosen the usual three annotations here.
@Test, @Before, @After
import java.lang .annotation Import Java.lang .annotation Import Java.lang .annotation Import Java.lang .annotation @Target (Value=elementtype)//Scope is @retention (Retentionpolicy)//Note information is persisted to runtime public @interface before {}
import java.lang .annotation Import Java.lang .annotation Import Java.lang .annotation Import Java.lang .annotation @Target (Value=elementtype)//Scope is @retention (Retentionpolicy)//Note information is persisted to runtime public @interface Test {}
import java.lang .annotation Import Java.lang .annotation Import Java.lang .annotation Import Java.lang .annotation @Target (Value=elementtype)//Scope is @retention (Retentionpolicy)//Note information is persisted to runtime public @interface after {}
Ii. define an annotated information processing process
Here, a reflection mechanism is used to get the object annotated method, and the method is activated.
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; Public class Junit {//Pass in an object instance Public Static voidRun (Class c) throws Instantiationexception, Illegalaccessexception, IllegalArgumentException, Invoca tiontargetexception {method[] methods = C.getdeclaredmethods ();//Get a collection of methods for object declarationsList<method> testlist =NewArraylist<method> ();//@Test method collection for annotationsMethod Aftermethod =NULL;//@After annotation MethodsMethod Beforemethod =NULL;//@Before annotation Methods for(Method method:methods) {all methods of the//cyclic object declaration //If there is a @test annotation, add the method to the method collection of the test annotation if(Method.isannotationpresent (Test.class) {Testlist.add (method); }//If there is a @before annotation, reference the method if(Method.isannotationpresent (before.class) {Beforemethod = method; }//If there is a @after annotation, reference the method if(Method.isannotationpresent (after.class) {Aftermethod = method; } }//new an object instanceObject obj = c.newinstance ();//Reflection activation method for(Method m:testlist) {if(Beforemethod! =NULL) {Beforemethod.invoke (obj,NULL); } m.invoke (obj,NULL);if(Aftermethod! =NULL) {Aftermethod.invoke (obj,NULL); } } }
III. Create a new test class
Public classTestprductor {@Before Public void Testbefore() {System. out. println ("Test Before"); } @Test Public void Testadd() {System. out. println ("Test Add"); } @Test Public void Testdel() {System. out. println ("Test Delete"); } @After Public void Testafter() {System. out. println ("Test after"); }}
Four, run this test class
import java.lang.reflect.InvocationTargetException;publicclass JunitTest { publicstaticvoidmainthrows InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ Junit.run(TestPrductor.class); } }
Results
Program Source: http://download.csdn.net/detail/q5841818/8979275
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Implementing a custom JUnit (Jane) using annotations