使用套件时的JUnit 4 @BeforeClass和@AfterClass

使用套件时的JUnit 4 @BeforeClass和@AfterClass,第1张

使用套件时的JUnit 4 @BeforeClass和@AfterClass

在AllTests类中编写一个@BeforeClass方法,该方法将在启动套件时执行。

public class MyTests1 {     @BeforeClass    public static void beforeClass() {        System.out.println("MyTests1.beforeClass");    }    @Before    public void before() {        System.out.println("MyTests1.before");    }    @AfterClass    public static void afterClass() {        System.out.println("MyTests1.AfterClass");    }    @After    public void after() {        System.out.println("MyTests1.after");    }    @Test    public void test1() {        System.out.println("MyTests1.test1");    }    @Test    public void test2() {        System.out.println("MyTests1.test2");    }}public class MyTests2 {     @BeforeClass    public static void beforeClass() {        System.out.println("MyTests2.beforeClass");    }    @Before    public void before() {        System.out.println("MyTests2.before");    }    @AfterClass    public static void afterClass() {        System.out.println("MyTests2.AfterClass");    }    @After    public void after() {        System.out.println("MyTests2.after");    }    @Test    public void test1() {        System.out.println("MyTests2.test1");    }    @Test    public void test2() {        System.out.println("MyTests2.test2");    }}@RunWith(Suite.class)@Suite.SuiteClasses( { MyTests1.class, MyTests2.class })public class AllTests {    @BeforeClass    public static void beforeClass() {        System.out.println("AllTests.beforeClass");    }    @Before    public void before() {        System.out.println("AllTests.before");    }    @AfterClass    public static void afterClass() {        System.out.println("AllTests.AfterClass");    }    @After    public void after() {        System.out.println("AllTests.after");    }    @Test    public void test1() {        System.out.println("AllTests.test1");    }    @Test    public void test2() {        System.out.println("AllTests.test2");    }}

输出值

AllTests.beforeClassMyTests1.beforeClassMyTests1.beforeMyTests1.test1MyTests1.afterMyTests1.beforeMyTests1.test2MyTests1.afterMyTests1.AfterClassMyTests2.beforeClassMyTests2.beforeMyTests2.test1MyTests2.afterMyTests2.beforeMyTests2.test2MyTests2.afterMyTests2.AfterClassAllTests.AfterClass

hth



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5479014.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存