java - 如何在 Junit3 中获得等效的 @BeforeClass 和 @AfterClass?

标签 java android junit junit4 junit3

我想在用测试夹具替换它之前备份我的应用程序的数据库。由于 Android 的限制,我不得不使用 Junit3,我想实现 @BeforeClass 和 @AfterClass 的等效行为。

UPDATE: There is now a tool (Junit4Android) to get support for Junit4 on Android. It's a bit of a kludge but should work.

为了实现与@BeforeClass 等效,我一直在使用静态变量并在第一次运行期间对其进行初始化,但我需要能够在运行所有测试后恢复数据库。我想不出检测上次测试何时运行的方法(因为我相信无法保证测试执行的顺序。)

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

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

最佳答案

来自junit website :

Wrapped the setUp and tearDown method in the suite.This is for the case if you want to run a single YourTestClass testcase.

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 ");
        }
    };
}

If you would like to run only one setUp and tearDown for all the testcase, make a suite and add testClass to it and pass the suite object in TestSetup constructor.But I think there is not much usage for this,and in a way it is violating JUnit philosophy.

关于java - 如何在 Junit3 中获得等效的 @BeforeClass 和 @AfterClass?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7208593/

相关文章:

java - 使用私有(private)成员或公共(public)访问器的方法

android - 跟踪位置时启用 Wi-Fi 会消耗更多还是更少的电池电量?

Android - 可配置的目录?

android - 使用 Android Intent 播放多首歌曲

java - EasyMock 和修改可变方法参数

java - 如何在 Java 中使用 Hamcrest 来测试异常?

java - android 错误 : java. lang.NullPointerException

java - 锁定文件写入?

java - java8中使用volatile实现Acquire/Release模型

java - 从 0.7 升级到 0.8.1.1 后生成嵌入式 kafka 队列时出错