Customize Java Annotations (iii) implement @test, @Before, @After in JUnit

Source: Internet
Author: User

The previous two custom annotations, all just parse a note, today's junit needs three annotations, and in the parsing framework to reflect the activation method in accordance with the before, Test, after the order of execution, it should be noted that the Test annotation may be more than So we define an array outside to store all the methods that are labeled by @test. Here's a look at the specific implementation

    1. Definition of three annotations
@Documented@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Inheritedpublic @interface Before {}
@Documented@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Inheritedpublic @interface Test {}
@Documented@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Inheritedpublic @interface After {}
    1. Use the custom three annotations in your own test class
 Public classmyjunittest {@Before Public void Prepare() {System. out. println ("Before: This method is performed before all test methods"); } @After Public void Destroy() {System. out. println ("Destroy: This method should be executed after all test methods"); } @Test Public void Testadd() {System. out. println ("Test:testadd ()"); } @Test Public void testupdate() {System. out. println ("Test:testupdate ()"); }}
    1. The most important annotation parsing framework
 Public classParsejunit { Public void Parsemethod(Class clazz) throws Instantiationexception, Illegalaccessexception, IllegalArgumentException, INVOC        Ationtargetexception, Nosuchmethodexception, SecurityException {Object obj = clazz.newinstance (); Method[] methods = Clazz.getdeclaredmethods ();//before annotations have only one, directly outside the definition of a method type of variableMethod methodsbefore=NULL; Method[] Methodstest=NULL;intindex=0; Method methodsafter=NULL;//Loop all the methods         for(Method m:methods) {//Get all annotations on the method, because unsure whether it is before or after, Test, cannot be directly annotated according to classAnnotation[] Myjunittest =m.getannotations ();//loop all annotations, match by name             for(intI=0; i<myjunittest.length;i++) {//Match the type of annotations by name                if(myjunittest[0].annotationtype (). Getsimplename (). EndsWith ("Before")) {methodsbefore=m; }Else if(myjunittest[0].annotationtype (). Getsimplename (). EndsWith ("Test")){//If Test, determine if this array is empty, no: in the defined array and then subscript plus a                    if(methodstest==NULL){//Yes, freshmen into an array, and then deposit m intomethodstest=Newmethod[Methods.length];                    } methodstest[index]=m;                index++; }Else if(myjunittest[0].annotationtype (). Getsimplename (). EndsWith ("after")) {methodsafter=m; }            }        }///1, first to implement before annotation method        if(methodsbefore!=NULL) {Methodsbefore.invoke (obj); }///2, the method in the array that executes the test annotation        if(methodstest!=NULL&& methodstest.length>0){ for(Method m:methodstest) {if(m!=NULL) {M.invoke (obj); }            }        }///3, the last method to execute after annotations        if(methodsafter!=NULL) {Methodsafter.invoke (obj); }    }}

May not be the best operating efficiency, but the basic effect has been achieved, the small partners have a better solution, please advise AH
4. Let's test the custom JUnit

publicvoidtestAppthrows InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException    {        ParseJunit pj=new ParseJunit();        pj.parseMethod(MyJunitTest.class);    }

Results:

Custom annotations, the most important part is the implementation of the resolution framework, the above mentioned three cases are based on the method of annotations, interested partners can find class annotations, attribute annotation parsing.

Copyright NOTICE: This article for Bo Master original article, thank you for reference! Where there is a problem, you are welcome to correct and progress together.

Customize Java Annotations (iii) implement @test, @Before, @After in JUnit

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.