Junit基础标签–执行顺序及解释
因为之前面试笔试题做到了,但平时使用的时候只用了@Test注解,现在来稍微仔细看一下.
Junit最基础的5个标签
@BeforeClass
@AfterClass
@Before
@After
@Test
对于这5个最基础的标签,其执行顺序是:
beforeClass -> before -> test -> after -> afterClass
测试代码
import org.junit.*;
/**
* @ClassName junitTest
* @Description Todo
* @Date 2020-11-02 10:32
* @Author mufei
* @Version 1.0
**/
public class junitTest {
@Before
public void test1(){
System.out.println("before");
}
@After
public void test2(){
System.out.println("after");
}
@AfterClass
public static void test3(){
System.out.println("afterclass");
}
@BeforeClass
public static void test4(){
System.out.println("beforeClass");
}
@Test
public void test(){
System.out.println("test");
}
}
执行结果为:
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 <code>public static void</code> no-arg method
* with <code>@BeforeClass</code> causes it to be run once before any of
* the test methods in the class. The <code>@BeforeClass</code> methods of superclasses
* will be run before those of the current class, unless they are shadowed in the current class.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface BeforeClass {
}
手写翻译(手动🐶):
有的时候一些测试需要共用一些在计算上代价很大的配置(例如链接数据库). 虽然这会损害测试的独立性,但是有时候这种优化是必要的.用@BeforeClass注解来标注一个无参的public static void方法,能够让它在这个类里的所有的测试方法之前执行一次.
父类中的@BeforeClass注解标注的方法,会在所有子类的@BeforeClass方法之前执行,除非父类中的方法被隐藏(?).
Before
官方注释
/**
* When writing tests, it is common to find that several tests need similar
* objects created before they can run. Annotating a <code>public void</code> method
* with <code>@Before</code> causes that method to be run before the {@link org.junit.Test} method.
* The <code>@Before</code> 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.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Before {
}
在编写测试时,经常会发现一些测试方法在运行之前需要创建类似的对象.用@Before标注一个public void方法可以让这个方法在@Test方法之前执行.
@Before标注的在父类中的方法要比当前类的方法之前执行,除非它们在当前类中被重写.
public class junitTest extends junitSuperClass {
@Before
public void test1(){
System.out.println("child before");
}
@Override
public void beforeOverride() {
System.out.println("child -- beforeOverride");
}
@Test
public void test(){
System.out.println("test");
}
}
public class junitSuperClass {
@Before
public void before(){
System.out.println("super before");
}
@Before
public void beforeOverride(){
System.out.println("super -- beforeOverride");
}
}
此时的执行结果为:
After
/**
* If you allocate external resources in a {@link org.junit.Before} method you need to release them
* after the test runs. Annotating a <code>public void</code> method
* with <code>@After</code> causes that method to be run after the {@link org.junit.Test} method. All <code>@;After</code>
* methods are guaranteed to run even if a {@link org.junit.Before} or {@link org.junit.Test} method throws an
* exception. The <code>@After</code> methods declared in superclasses will be run after those of the current
* class, unless they are overridden in the current class.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface After {
}
如果在@Before方法中配置了外部资源,需要在@After方法中释放资源.在public void方法上添加@After注解可以让这个方法在@Test方法之后执行.即使发生了异常,所有的@After方法都会保证在最后被执行.除非@After方法在当前类中被重写,父类中的@After方法会在当前类的@after方法之后执行.
AfterClass
/**
* If you allocate expensive external resources in a {@link org.junit.BeforeClass} method you need to release them after all the tests in the class have run. Annotating a <code>public static void</code> method with <code>@AfterClass</code> causes that method to be run after all the tests in the class have been run. All <code>@AfterClass</code> methods are guaranteed to run even if a {@link org.junit.BeforeClass} method throws an exception. The <code>@AfterClass</code> methods declared in superclasses will be run after those of the current class, unless they are shadowed in the current class.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AfterClass {
}
如果在@BeforeClass方法中配置了外部的资源,需要在所有的测试方法执行结束之后释放.在使用了@AfterClass注解的public static void方法可以在最后执行.所有的@AfterClass方法保证在最后被执行无论在@BeforeClass方法中是否有异常.父类中的@AfterClass方法会在子类的@AfterClass方法之后执行,除非被子类重写.