Implementing a custom JUnit (Jane) using annotations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.