JUnit中标注Annotation介绍

来源:互联网 发布:中国网络直播平台排名 编辑:程序博客网 时间:2024/06/13 03:45

java标注(Annotation)

对于Annotation,是Java5的新特性,JDK5引入了Metedata(元数据)很容易的就能够调用Annotations。Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形式应用于代码:类(class),属性(field),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。

来源:百度知道Annotation


JUnit 4的Annotation

Junit 4.x实现了一些Annotation,主要有Test, Ignore, BeforeClass, AfterClass, Before, After, RunWith, SuiteClasses, 和Parameters。

参考:http://junit.sourceforge.net/javadoc/overview-tree.html

  • Test

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.

@Test标注告诉JUnit它所附属的public void方法将作为一个测试案例来运行。Test标注支持两个可选的参数,分别是expected(测试方法要抛出异常,若没有则失败)和timeout(耗费时间超过给定的时间ms就失败)

@Test(expected=IndexOutOfBoundsException.class)

@Test(timeout=100)

  • Ignore

 如果你暂时想让一个测试或者一组测试不执行,那么标注为Test的方法如果也标注了@Ignore的话就不会被作为测试被执行。如果你对包含有测试方法的类标注@Ignore的话,它所包含的测试均不执行。JUnit会给出忽略的测试数以及运行的和失败的数目。@Ignore可以携带一个可选的默认参数来说明这个测试为什么被忽略。

@Ignore("not ready yet") @Test public void something() { ...

  • BeforeClass/AfterClass

有时一些测试需要共同的初始设置,如连接数据库,它们有比较高的计算复杂度。因为它们独立于测试,可以对其进行优化。对一个不带参数的public static void方法标注为@BeforeClass,能够让它在类中所有测试执行前仅执行一次。而父类的@BeforeClass会先与当前执行。

同理,我们对@BeforeClass中申请的外部资源需要在测试执行完后释放。@AfterClass所标记的public static void方法会在所有测试执行完后执行。纵然BeforeClass方法抛出异常,AfterClass也会被执行。父类中的AfterClass方法将晚于当前类被执行。

  • Before/After

当写测试时常会发现很多测试在执行前需要建立相似的对象。于是可以用@Before标记的public void方法来实现这个功能,使得该方法先于测试执行。父类的Before方法先于当前类执行。

类似于AfterClass和BeforeClass的关系,Before中申请的资源将在@After标记的public void方法中释放。@After在每个测试执行完后执行,@Before中有异常@After也照常执行,父类的该方法晚于当前类被执行。

  • RunWith

当一个类标记为@RunWith或者继承了一个标记了@RunWith的类,JUnit就会触发它所指代的runner类来运行那个类中的测试而不需要JUnit内置的Runner。主要的runner如:JUnit38ClassRunnerParameterizedSuite,JUnit4ClassRunner等

  • SuiteClasses

SuiteClasses标注指定 当标注为@RunWith(Suite.class)的类正在运行时 所要运行的测试类

@RunWith(value = Suite.class)
@SuiteClasses(value={CalculatorComputerSuite.class, SomeTest.class})

表示要对CalculatorComputerSuiteSomeTest中的suite运行测试,如果没有指定suite则会自动找到类中所有的test方法或者@Test标记

  • Parameters

用来标注一个能够通过Parameterized提供参数到测试类构造器中的方法(参数化测试)


@RunWith(Suite.class) 

@SuiteClasses(ATest.class, BTest.class, CTest.class) 

public class ABCSuite { }


@RunWith(Parameterized.class)  

public class ParameterTest { 

 ……

@Parameters  

public static Collection prepareData(){  …… }

……

}




一般在一个测试类中执行的Test和Before/After及BeforeClass和AfterClass顺序为:

BeforeClass=>[Before->Test->After](多少个测试就会有多少次) => AfterClass



public class MathTest {
    
    public MathTest(){
        System.out.println("Create MathTest instance ...");
    }

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("Call @BeforeClass");
    }
    
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("Call @AfterClass");
    }
    
    @Before
    public void setUp() throws Exception {
        System.out.println("Call @Before before a test case!");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("Call @After after a test case!");
    }

    @Test
    public void testDiv() {
        Math math = new Math();
        assertEquals(2,math.div(9, 4));
        assertEquals(5,math.div(100, 20));
    }

    @Test
    public void testExp() {
         Math math = new Math();
         assertEquals(32f, math.exp(2, 5), 0.001f);
         assertEquals(1f, math.exp(2, 0), 0.001f);
         assertEquals(0.5f, math.exp(2, (-1)), 0.001f);   
    }
    
    @Ignore("Not for the test class")
    @Test(timeout=1)
    public void testTimeLimitedExceeded() {
        double d = 0;
        for(int i = 0;i < 10000000; i ++)
            d += i;
    }
    
    @Test(expected=ArithmeticException.class)
    public void testDivByZero() {
        System.out.println("Started test divided by 0...");
        new Math().div(1, 0);
    }

}