您的位置:首页 > 编程语言 > Java开发

Junit和javadoc的完美结合

2009-12-29 13:26 555 查看
实际遇到的问题:
在编写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();
}
}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息