Junit和javadoc的完美结合

来源:互联网 发布:python分布式爬虫框架 编辑:程序博客网 时间:2024/05/22 04:29

实际遇到的问题:

在编写junit的测试代码的时候,需要为每个Case编写注释作者等信息。而执行结果平台也需要知道这些信息,而不是通过查看测试代码来发现这些信息,这就需要在测试执行的时候读取到javadoc的信息。

本文就是针对这个问题的解决方案

首先,由于JavaDoc是只能读取紧贴函数的注释,目前我们Java代码中都被@Test占据。所以我想到只能修改@Test接口

         于是将Junit@Test接口修改如下:

public @interface Test {

   

    /**

     * Default empty exception

     */

    static class None extends Throwable {

       private static final long serialVersionUID= 1L;     

       private None() {

       }

    }

   

    /**

     * Optionally specify <code>expected</code>, a Throwable, to cause a test method to succeed iff

     * an exception of the specified class is thrown by the method.

     */

    Class<? extends Throwable> expected() default None.class;

   

    /**

     * Optionally specify <code>timeout</code> in milliseconds to cause a test method to fail if it

     * takes longer than that number of milliseconds.*/

    long timeout() default 0L;

   

    String author(); //新增

   

    String description(); //新增

}

  新增了作者和注释两个新的属性;

 应用代码(即编写测试用例的时候,在@Test中必须注明作者和描述信息属性)如下:

public class TestTag {

   

    @Test(author="elbert.chenh",description="TestCase1")

    public void TestCase1()

    {

       System.out.println("OK");

    }

}

 

测试代码如下:

public class TestTestTag {

    public static void main(String[] args) {

       TestTag tt = new TestTag();

       try {

           Annotation[] annotation = tt.getClass().getMethod("TestCase1")

                  .getAnnotations();

           for (Annotation tag : annotation) {

              System.out.println("Tag is:" + tag);

              System.out.println("tag.name()" + ((Test) tag).author());

              System.out.println("tag.age()" + ((Test) (tag)).description());

           }

       } catch (NoSuchMethodException e) {

           e.printStackTrace();

       }

    }

 

}

 

原创粉丝点击