JUnit 4 Migration

As of Android 24 (N), JUnit3 style javatests have been deprecated for the new JUnit4-based Android Testing Support Library. We are in the process of changing all instrumentation tests in chromium to JUnit4 style. This doc explains the differences between JUnit3 and JUnit4 instrumentation tests and how to write or convert them.

Differences between JUnit3 and JUnit4 instrumentation tests

JUnit3JUnit4
InheritanceTests extends TestCase or child classesNo inheritance.
Test methodsmethods named with test prefixmethods annotated with @Test
Set upsetUp() methodpublic method annotated with @Before
Tear downtearDown() methodpublic method annotated with @After
Test runnerdeclared within test apk AndroidManifest.xmlMust specify chromium-junit4:"true"
Class runnerN/A@RunWith(XClassRunner.class)
AssertionExtends from junit.framework.Assert, inherited APIsUse static methods from org.junit.Assert

Please note that during the migration, we support running JUnit3 and JUnit4 tests in the same apk. This requires two tags, one each for JUnit3 and JUnit4. The tag for the JUnit4 runner must specify chromium-junit4:"true" (Example)

  • Other JUnit4 features:
    • Tests can be annotated to expect an exception, e.g. @Test(expected=MyException.class). Tests annotated this way will fail if they do not throw the given exception.
  • Test suite set up: public static method annotated with @BeforeClass
  • Test suite tear down: public static method annotated with @AfterClass
  • Replacement for JUnit3 test base classes
    • TestRule:

      • TestRule is a class to outsource your test setUp, tearDown, and utility methods. Since there are no more interitance and TestBase classes, one should use TestRule for any API calls provided by its test base classes previously.
      • One test can declare multiple TestRules and the class runner will run all of them. If the order of the TestRule matters to you, use RuleChain
    • ActivityTestRule

      • ActivityTestRule is a special TestRule provided by Android Testing Support Library that allows tests to launch an Activity. (Documentation)

Example Code of JUnit3 test and JUnit4 test

JUnit3:

public class MyTestClass extends MyActivityInstrumentationTestCase2<TestActivity> {
    @Override
    protected void setUp(){
        super.setUp();
        setActivityIntent(new Intent());
        getActivity();
    }

    @Override
    protected void tearDown() {
        specialActionFromSuper();
        super.tearDown();
    }

    public void testA() {
        assertEquals(1, 1);
    }
}

JUnit4:

@RunWith(BaseJUnit4ClassRunner.class);
public class TestClass {
    @Rule public ActivityTestRule<TestActivity> mRule = new ActivityTestRule<>(TestActivity.class);

    @Before
    public void setUp() { //Must be public
        mRule.launchActivity(new Intent());
    }

    @After
    public void tearDown() { //Must be public
        mRule.specialActionFromActivityTestRule();
    }

    @Test
    public void testA() {
        Assert.assertEquals(1, 1);
    }
}

Migration process

  1. Add required libraries to your target dependencies in BUILD.gn
    • JUnit 4 library: //third_party/junit
    • Android Testing Support Rules:
      • //third_party/android_support_test_runner:runner_java (for AndroidJUnitRunner, etc)
      • //third_party/android_support_test_runner:rules_java (for ActivityTestRule, etc)
  2. Add class runner to your test apk manifest. (example)
    • Keep in mind you can have multiple instrumentations in your manifest. Our test runner will run JUnit4 tests with JUnit4 runner and JUnit3 tests with non-JUnit4 runner.
  3. Refactor TestBase class to a TestRule class. (example CL)
    • +yolandyan will do this part, however, if you did refactoring yourself, please add him as a reviewer for your CL and enjoy his eternal appreciation!
  4. Use auto migrate script to or manually convert all JUnit3 tests to JUnit4 style in a your javatest directory
    • we understand it's tedious to just manually write all the annotations, change modifiers, etc to convert all the javatest, so we created an auto change script that helps you to convert all the javatests in a certain directory. Please check its README page on instructions.

Customized TestRule example

TestRule:

public class MyRule implements TestRule {
    // 1: Add utility methods...

    @Override
    public Statement apply(final Statement base, Description desc) {
        return new Statement() {
            @Override
            public void evaluate() {
                // 2: Code here runs before @Before method
                base.evaluate()
                // 3: Code here runs after @After method
            }
        }
    }
}

Command Line Flags

In our Junit3 tests command line flags (set by the CommandLineFlag annotations) were inherited from the test base classes. As an example, ChromeActivityTestBase is annotated with:

@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE, ...

and as a result any test in a class derived from ChromeActivityTestBase will disable the first run experience.

The Junit4 tests classes are not however, derived from test base classes; instead their behavior is defined by test rules. To support this our Junit4 test runner will examine the command line flag annotations on all rules referenced with @Rule annotations in the test class. In addition, where one rule is derived from another, the command line flags propogate through the hierarchy of rules. See, for example, BottomSheetTestRule

Note:- This has only recently been implemented, so is not yet used in all tests. See this bug

The CommandLineFlags annonations are more fully documented in the CommandLineFlags class

Common Errors

  1. Instrumentation tests that rely on test thread to have message handler will not work. For example error message:

    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    

    or

    java.lang.IllegalStateException: The current thread must have a looper!
    

    Please utilize ActivityTestRule.runOnUiThread(Runnable r) to refactor these tests. For more, check this GitHub issue

  2. Use @UiThreadTest with caution!!

    • Currently, @UiThreadTest is only effective when UiThreadTestRule or ActivityTestRule is declared in the test class.
    • Please use android.support.test.annotation.UiThreadTest, NOT android.test.UiThreadTest.
    • When using @UiThreadTest, it would cause setUp and tearDown to run in Ui Thread as well. Avoid that by calling runOnUiThread or runOnMainSync with a Runnable.
    // Wrong test
    public class Test {
        @Rule
        public ActivityTestRule<MyActivity> mRule = new ActivityTestRule<>(
                MyActivity.class>
    
        @Before
        public void setUp() {
            // Cause failure because this also runs on Ui Thread, while it
            // is intended for Instrumentation worker thread
            mRule.launchActivity() 
        }
    
        @UiThreadTest
        public void test() {
            actionThatNeedsUiThread();
        }
    }
    

    The correct thing to do is

    // Correct test
    public class Test {
        @Rule
        public ActivityTestRule<MyActivity> mRule = new ActivityTestRule<>(
                MyActivity.class>
    
        @Before
        public void setUp() {
            mRule.launchActivity()
        }
    
        public void test() {
            mRule.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    actionThatNeedsUiThread();
                }
            });
        }
    }
    
  3. assertEquals(float a, float b) and assertEquals(double a, double b) are deprecated in JUnit4's Assert class. Despite only generating a warning at build time, they fail at runtime. Please use Assert.assertEquals(float a, float b, float delta)

  4. Errorprone expects all public methods starting with test... to be annotated with @Test. Failure to meet that expectation will cause errorprone to fail with something like this:

    [JUnit4TestNotRun] Test method will not be run; please add @Test annotation
    

    In particular, you may see this when attempting to disable tests. In that case, the test should be annotated with both @DisabledTest and @Test.

Common questions

  • Q: Are @Test and @LargeTest/@MediumTest/@SmallTest annotation both necessary?
    • A: Yes, both are required for now. We plan to refactor this in the future.
  • Q: Isn't the inheritance of the Test classes just migrated to inheritance of TestRules?
    • A: Yes. During the migration, we plan to maintain a 1:1 mapping between the test base classes and TestRules (e.g. ContentShellTestBase to ContentShellTestRule in this CL). This allows the auto convert script to replace API calls in any JUnit3 tests. After the migration, we plan to refactor the TestRules to be more modular.

If you have any other questions, feel free to report in this bug.

Links and Crbugs