junit4xi注释详解

来源:互联网 发布:java new int 编辑:程序博客网 时间:2024/05/22 00:41

JUnit4注解基本介绍

@After

If you allocate external resources ina Before method you need to release them after the test runs.Annotating apublic void method with @After causes that method to be run after theTest method. All @After methods are guaranteed to run even if aBefore or Test method throws an exception. The @After methodsdeclared in superclasses will be run after those of the current class. 

如果在@Before注解方法中分配了额外的资源,那么在测试执行完后,需要释放分配的资源。

使用@After注解一个publicvoid方法会使该方法在@Test注解方法执行后被执行

即使在@Before注解方法、@Test注解方法中抛出了异常,所有的@After注解方法依然会被执行,见示例

父类中的@After注解方法会在子类@After注解方法执行后被执行

[java]view plaincopyprint?

1.  public class MathTest {  

2.      @Before  

3.      public void setUp() throws Exception {  

4.          throw new Exception();  

5.      }  

6.    

7.      @Test  

8.      public void testAdd() {  

9.          Math m = new Math();  

10.         assertTrue(m.add(11) == 2);  

11.     }  

12.       

13.     @After  

14.     public void tearDown() throws Exception {  

15.         System.out.println("after");  

16.     }  

17. }  

public class MathTest {

@Before

publicvoid setUp() throws Exception {

  thrownew Exception();

}

 

@Test

publicvoid testAdd() {

  Mathm = new Math();

  assertTrue(m.add(1,1) == 2);

}

@After

publicvoid tearDown() throws Exception {

  System.out.println("after");

}

}

[plain]view plaincopyprint?

1.  after  

after

 

@AfterClass

If you allocate expensive externalresources in a Before Class method you need to release them after all thetests in the class have run. Annotating a public static void method with@AfterClass causes that method to be run after all the tests in the class havebeen run. All @AfterClass methods are guaranteed to run even if a Before Classmethod throws an exception.The @AfterClass methods declared in superclasseswill be run after those of thecurrent class.

如果在@BeforeClass注解方法中分配了代价高昂的额外的资源,那么在测试类中的所有测试方法执行完后,需要释放分配的资源。

使用@AfterClass注解一个publicstatic void方法会使该方法在测试类中的所有测试方法执行完后被执行

即使在@BeforeClass注解方法中抛出了异常,所有的@AfterClass注解方法依然会被执行

父类中的@AfterClass注解方法会在子类@AfterClass注解方法执行后被执行

 

@Before

When writing tests, it is common tofind that several tests need similar objects created before they can run.Annotating a public void method with @Before causes that method to be runbefore the Test method. The @Before methods of superclasses will be run beforethose of the current class. No other ordering is defined.

当编写测试方法时,经常会发现一些方法在执行前需要创建相同的对象

使用@Before注解一个publicvoid方法会使该方法在@Test注解方法被执行前执行(那么就可以在该方法中创建相同的对象)

父类的@Before注解方法会在子类的@Before注解方法执行前执行

 

@BeforeClass

Sometimes several tests need to sharecomputationally expensive setup (like logging into a database). While this cancompromise the independence of tests, sometimes it is a necessaryoptimization.Annotating a public static void no-arg method with @BeforeClasscauses it to be run once before any of the test methods in the class. The@BeforeClass methods of superclasses will be run before those the currentclass.

有些时候,一些测试需要共享代价高昂的步骤(如数据库登录),这会破坏测试独立性,通常是需要优化的

使用@BeforeClass注解一个publicstatic void方法,并且该方法不带任何参数,会使该方法在所有测试方法被执行前执行一次,并且只执行一次

父类的@BeforeClass注解方法会在子类的@BeforeClass注解方法执行前执行

 

@Ignore

Sometimes you want to temporarilydisable a test or a group of tests. Methods annotated with Test that arealso annotated with @Ignore will not be executed as tests. Also, you canannotate a class containing test methods with @Ignore and none of thecontaining tests will be executed. Native JUnit 4 test runners should reportthe number of ignored tests along with the number of tests that ran and thenumber of tests that failed.

对包含测试类的类或@Test注解方法使用@Ignore注解将使被注解的类或方法不会被当做测试执行

JUnit执行结果中会报告被忽略的测试数

[java]view plaincopyprint?

1.  public class MathTest {  

2.      @Ignore("do not test")  

3.      @Test  

4.      public void testAdd() {  

5.          Math m = new Math();  

6.          assertTrue(m.add(11) == 2);  

7.      }  

8.  }  

public class MathTest {

@Ignore("donot test")

@Test

publicvoid testAdd() {

  Mathm = new Math();

  assertTrue(m.add(1,1) == 2);

}

}

[java]view plaincopyprint?

1.  @Ignore  

2.  public class MathTest {  

3.      @Test  

4.      public void testAdd() {  

5.          Math m = new Math();  

6.          assertTrue(m.add(11) == 2);  

7.      }  

8.  }  

@Ignore

public class MathTest {

  @Test

  publicvoid testAdd() {

    Mathm = new Math();

    assertTrue(m.add(1,1) == 2);

  }

}

执行结果相同:

 

@Test

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

The Test annotation supports twooptional parameters. 

The first, expected,declares that atest method should throw an exception. If it doesn't throw an exception or ifit throws a different exception than the one declared, the test fails. 

The second optional parameter,timeout, causes a test to fail if it takes longer than a specified amount ofclock time (measured in milliseconds). 

@Test注解的public void方法将会被当做测试用例

JUnit每次都会创建一个新的测试实例,然后调用@Test注解方法

任何异常的抛出都会认为测试失败

@Test注解提供2个参数:

1“expected”,定义测试方法应该抛出的异常,如果测试方法没有抛出异常或者抛出了一个不同的异常,测试失败

2“timeout”,如果测试运行时间长于该定义时间,测试失败(单位为毫秒)

[java]view plaincopyprint?

1.  public class MathTest {  

2.      @Test(expected=Exception.class)  

3.      public void testAdd() throws Exception{  

4.          throw new Exception();  

5.      }  

6.  }  

public class MathTest {

                    @Test(expected=Exception.class)

                   publicvoid testAdd() throws Exception{

                                       thrownew Exception();

                   }

}

[java]view plaincopyprint?

1.  public class MathTest {  

2.      @Test(timeout=5000)  

3.      public void testAdd() {  

4.          for(;;){  

5.                

6.          }  

7.      }  

8.  }  

 

 

public class MathTest {

                @Test(timeout=5000)

                     publicvoid testAdd() {

                     for(;;){

                      }

           }

}

JUnit4使用Java5中的注解(annotation),以下是JUnit4常用的几个annotation 
@Before:初始化方法   对于每一个测试方法都要执行一次(注意与BeforeClass区别,后者是对于所有方法执行一次)
@After:释放资源  对于每一个测试方法都要执行一次(注意与AfterClass区别,后者是对于所有方法执行一次)
@Test:测试方法,在这里可以测试期望异常和超时时间 
@Test(expected=ArithmeticException.class)检查被测方法是否抛出ArithmeticException异常 
@Ignore:忽略的测试方法 
@BeforeClass针对所有测试,只执行一次,且必须为static void 
@AfterClass针对所有测试,只执行一次,且必须为static void 
一个JUnit4的单元测试用例执行顺序为: 
@BeforeClass -> @Before -> @Test ->@After -> @AfterClass; 
每一个测试方法的调用顺序为: 

@Before-> @Test -> @After; 

 

 

0 0