JUnit使用(二)

来源:互联网 发布:腾讯代理绝地求生优化 编辑:程序博客网 时间:2024/06/06 04:21

JUnit运行流程

1.@BeforeClass修饰的方法会在所有方法被调用之前运行,而且该方法是静态的(static修饰),当测试类被加载后就运行了它,在内存中只会存在一份实例,比较适合修饰配置文件.

2.@AfterClass所修饰的方法通常用来进行对资源的管理(static修饰).

3.@Before和@After在每个测试方法的前后各运行一次

测试代码:

import static org.junit.Assert.*;import java.text.Collator;import org.junit.After;import org.junit.AfterClass;import org.junit.Before;import org.junit.BeforeClass;import org.junit.Test;import com.yangshunfan.JUnit.Math;public class MathTest {@AfterClasspublic static void afterClass() {System.out.println("类之前运行");}@BeforeClasspublic static void beforeClass() {System.out.println("类之后运行");}@Afterpublic void after() {System.out.println("方法之前运行");}@Beforepublic void before() {System.out.println("方法之后运行");}@Testpublic void test1(){System.out.println("这是方法1");}@Testpublic void test2(){System.out.println("这是方法2");}}


0 0
原创粉丝点击