How can I get @BeforeClass and @AfterClass equival

2019-02-12 15:47发布

问题:

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.

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

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();
        }
    }
    ...
}

回答1:

From the 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.



回答2:

Recently, I was looking for a similar solution too. Fortunately, in my case after the JVM exits after the last test is run. So I was able to achieve this by adding a JVM shutdown hook.

// Restore database after running all tests
Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        restoreDatabase();
    }
});

hope this helps.



回答3:

Isn't this (dealing elegantly with data, so you don't have to worry about restoring it) what testing with mock objects are for? Android supports mocking.

I ask as a question, since I've never mocked Android.


In my experiences, and from this blog post, when the Android tests are made into a suite and run by the InstrumentationTestRunner - ActivityInstrumentationTestCase2 is an extension of ActivityTestCase which is an extendsion of InstrumentationTestCase - they are ordered alphabetically using android.test.suitebuilder.TestGrouping.SORT_BY_FULLY_QUALIFIED_NAME, so you can just restore you DB with a method that is the lowes in the alphabet out of your test names, like:

// underscore is low in the alphabet
public void test___________Restore() { 
    ... 
}

Note:

You have to pay attention to inherited tests, since they will not run in this order. The solution is to override all inherited test and simply call super() from the override. This will once again have everything execute alphabetically.

Example:

// Reusable class w only one time setup and finish. 
// Abstract so it is not run by itself.
public abstract class Parent extends InstrumentationTestCase {
    @LargeTest
    public void test_001_Setup() { ... }

    @LargeTest
    public void test_____Finish() { ... }
}

/*-----------------------------------------------------------------------------*/

// These will run in order shown due to naming.
// Inherited tests would not run in order shown w/o the use of overrides & supers
public class Child extends Parent {
    @LargeTest
    public void test_001_Setup() { super.test_001_Setup(); }

    @SmallTest
    public void test_002_MainViewIsVisible() { ... }

    ...

    @LargeTest
    public void test_____Finish() { super.test_____Finish(); }
}


回答4:

I would suggest avoiding these kind of dependencies where you need to know the order in which tests are run. If all you need is to restore a real database that was replaced by setUpDatabaseFixture() probably you solution comes from the use of a RenamingDelegatingContext. Anyway, if you can't avoid knowing when the last test was run, you can use something like this:

...

private static final int NUMBER_OF_TESTS = 5; // count your tests here
private static int sTestsRun = 0;

...

protected void tearDown() throws Exception {
    super.tearDown();
    sTestsRun += countTestCases();

    if ( sTestsRun >= NUMBER_OF_TESTS ) {
        android.util.Log.d("tearDow", "*** Last test run ***");
    }
}