场景:
使用JUnit测试类的时候会使用到的,比较实用的几个注解。主要是用来控制执行顺序的。
顺序结果:
@BeforeClass -> @Before -> @Test -> @After -> @AfterClass;
@BeforeClass
Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization. Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those of the current class, unless they are shadowed in the current class.
For example:
public class Example {
@BeforeClass public static void onlyOnce() {
…
}
@Test public void one() {
…
}
@Test public void two() {
…
}
}
大致翻译:
有时,一些测试需要共享计算成本高昂的设置(如登录数据库)。虽然这可能会损害测试的独立性,但有时这是一种必要的优化。用@BeforeClass注释公共静态void no arg方法会导致它在类中的任何测试方法之前运行一次。超类的@BeforeClass方法将在当前类的方法之前运行,除非它们在当前类中被隐藏。
@Before
官方注释
When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before the Test method. The @Before methods of superclasses will be run before those of the current class, unless they are overridden in the current class. No other ordering is defined.
Here is a simple example:
public class Example {
List empty;
@Before public void initialize() {
empty= new ArrayList();
}
@Test public void size() {
…
}
@Test public void remove() {
…
}
}
大致翻译:
编写测试时,通常会发现一些测试在 运行之前 需要创建类似的对象。
用@Before注释公共void方法会导致该方法在 测试方法之前 运行。
超类的@Before方法将在当前类的方法之前运行,除非它们在当前类中被重写。没有定义其他顺序。
@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.
A simple test looks like this:
public class Example {
@Test
public void method() {
org.junit.Assert.assertTrue( new ArrayList().isEmpty() );
}
}
大致翻译:
@Test注释告诉JUnit它所附加的public void方法可以作为测试用例运行。为了运行该方法,JUnit首先构造一个新的类实例,然后调用带注释的方法。测试引发的任何异常都将由JUnit报告为失败。如果没有抛出异常,则假定测试已成功
@After
If you allocate external resources in a Before method you need to release them after the test runs. Annotating a public void method with @After causes that method to be run after the Test method. All @After methods are guaranteed to run even if a Before or Test method throws an exception. The @After methods declared in superclasses will be run after those of the current class, unless they are overridden in the current class.
Here is a simple example:
public class Example {
File output;
@Before public void createOutputFile() {
output= new File(…);
}
@Test public void something() {
…
}
@After public void deleteOutputFile() {
output.delete();
}
}
大致翻译:
如果在@Before方法中分配外部资源,则需要 在测试运行后释放它们。用@After注释公共void方法会导致该方法在测试方法之后运行。**即使Before或Test方法抛出异常,所有@After方法都保证运行。**超类中声明的@After方法将在当前类的方法之后运行,除非它们在当前类中被重写。
@AfterClass
If you allocate expensive external resources in a BeforeClass method you need to release them after all the tests 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 have been run. All @AfterClass methods are guaranteed to run even if a BeforeClass method throws an exception. The @AfterClass methods declared in superclasses will be run after those of the current class, unless they are shadowed in the current class.
Here is a simple example:
public class Example {
private static DatabaseConnection database;
@BeforeClass public static void login() {
database= …;
}
@Test public void something() {
…
}
@Test public void somethingElse() {
…
}
@AfterClass public static void logout() {
database.logout();
}
}
大致翻译:
如果在BeforeClass方法中分配昂贵的外部资源,则需要在类中的所有测试运行后释放它们。用@AfterClass注释公共静态void方法会导致在类中的所有测试都已运行后运行该方法。即使BeforeClass方法引发异常,所有@AfterClass方法都保证运行。超类中声明的@AfterClass方法将在当前类的方法之后运行,除非它们在当前类中被隐藏。