我想在用测试夹具替换它之前备份我的应用程序的数据库。由于 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/