本文介绍了我怎样才能获得@BeforeClass和@AfterClass相当于Junit3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与测试装置取代它之前备份我的应用程序的数据库。我被迫使用,因为Android的限制Junit3,我想实现@BeforeClass的@AfterClass相当于行为。

要实现@BeforeClass等价,我一直在使用一个静态变量,这样在第一次运行时初始化它,但我需要能够运行所有的测试后恢复数据库。我想不出检测当最后测试已经运行的方式(因为我相信没有保证测试执行的顺序。)

 公共类MyTest的扩展ActivityInstrumentationTestCase2< MainActivity> {
    私有静态布尔firstRun = TRUE;

    @覆盖
    保护无效设置(){
        如果(firstRun){
            firstRun = FALSE;
            setUpDatabaseFixture();
        }
    }
    ...
}
 

解决方案

JUnit的网站

 公共静态测试套件(){
    返回新TestSetup(新的TestSuite(YourTestClass.class)){

        保护无效设置()抛出异常{
            的System.out.println(全局设置);
        }
        保护无效tearDown的()抛出异常{
            的System.out.println(环球tearDown的);
        }
    };
}
 

I want to back up my application's database before replacing it with the test fixture. I'm forced to use Junit3 because of Android limitations, and I want to implement the equivalent behavior of @BeforeClass an @AfterClass.

To achieve the @BeforeClass equivalent, I had been using a static variable and initializing it during the first run like this, but I need to be able to restore the database after running all the tests. I can't think of a way of detecting when the last test has run (since I believe there is no guarantee on the order of test execution.)

public class MyTest extends ActivityInstrumentationTestCase2<MainActivity> {
    private static boolean firstRun = true;

    @Override
    protected void setUp() {
        if(firstRun) {
            firstRun = false;
            setUpDatabaseFixture();
        }
    }
    ...
}
解决方案

From the junit website:

public static Test suite() {
    return new TestSetup(new TestSuite(YourTestClass.class)) {

        protected void setUp() throws Exception {
            System.out.println(" Global setUp ");
        }
        protected void tearDown() throws Exception {
            System.out.println(" Global tearDown ");
        }
    };
}

这篇关于我怎样才能获得@BeforeClass和@AfterClass相当于Junit3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 04:47