@BeforeClass,@Before,@Test@After,@AfterClass使用

场景:

使用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方法将在当前类的方法之后运行,除非它们在当前类中被隐藏。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这些注解都是JUnit测试框架中的注解,用于声明测试用例方法和测试执行的前置条件、后置处理等。 - `@Test`:用于标注测试用例方法,JUnit框架会自动执行被该注解标记的方法。 - `@BeforeClass`:用于标注在所有测试用例执行前需执行的方法,通常用于执行一些初始化操作,被该注解标记的方法必须是静态方法。 - `@Before`:用于标注在每个测试用例执行前需执行的方法,通常用于执行一些初始化操作。 - `@After`:用于标注在每个测试用例执行后需执行的方法,通常用于执行一些清理操作。 - `@AfterClass`:用于标注在所有测试用例执行后需执行的方法,通常用于执行一些清理操作,被该注解标记的方法必须是静态方法。 下面是使用示例代码: ```java import org.junit.*; public class ExampleTest { @BeforeClass public static void beforeClass() { // 执行一些初始化操作 System.out.println("Before Class"); } @Before public void before() { // 执行一些初始化操作 System.out.println("Before"); } @Test public void test1() { // 执行测试用例1 System.out.println("Test1"); } @Test public void test2() { // 执行测试用例2 System.out.println("Test2"); } @After public void after() { // 执行一些清理操作 System.out.println("After"); } @AfterClass public static void afterClass() { // 执行一些清理操作 System.out.println("After Class"); } } ``` 在上面的示例中,`@BeforeClass`注解所标注的方法会在所有测试用例执行前执行一次,`@Before`注解所标注的方法会在每个测试用例执行前执行一次,`@Test`注解所标注的方法表示一个测试用例方法,`@After`注解所标注的方法会在每个测试用例执行后执行一次,`@AfterClass`注解所标注的方法会在所有测试用例执行后执行一次。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值