33

as the documentation of Android says, "Note that the Android testing API supports JUnit 3 code style, but not JUnit 4." (Testing Fundamentals). It should be clear that JUnit 4 cannot be used out of the box with Android.

But why is this the case? Is it because the tests are executed within the DVM (in that the Android Runtime only has support for JUnit 3)? On a JVM one on its own could choose the JUnit runtime that should be used. Isn't this possible within the DVM?

Matthew
  • 816
  • 1
  • 7
  • 10
  • 1
    Have you seen this question? Perhaps it's what youre looking for: http://stackoverflow.com/questions/2172152/cant-run-junit-4-test-case-in-eclipse – bsimic Mar 21 '12 at 17:33
  • 1
    thanks but no actually i'm not looking for this solution. Of course using this solution it is possible to test POJOs with JUnit 4. But it is impossible to use it for code units relying on Android Framework Classes (i.e. are running on the device itself and not on the JVM on my development machine) I'm quite sure it is hardly possible to run JUnit 4 tests in the DVM but i'm looking for an explanation WHY this is the case. – Matthew Mar 21 '12 at 22:52
  • I don't know why its the case besides that the Android Unit testing implementations extend the JUnit3 libraries. I'm sure that future releases of the android development kit will support 4. – bsimic Mar 21 '12 at 23:25

2 Answers2

40

Update 2015/10

It is now possible via the AndroidJUnitRunner, which is part of the Android Testing Support Library. In short, you need to do the following in a Gradle-based project:

  1. Add the dependencies:

    dependencies {
        compile 'com.android.support:support-annotations:22.2.0'
        androidTestCompile 'com.android.support.test:runner:0.4.1'
    }
    
  2. Specify the testInstrumentationRunner:

    android {
        defaultConfig {
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    }
    
  3. Annotate your test class with @RunWith(AndroidJUnit4.class).

  4. Write your tests in normal JUnit4 style.

Also see the Espresso setup instructions. Note that you don't need Espresso itself for plain JUnit4 tests.

Why it's needed

There are very little information I could find on the internet on this topic. The best I found was the info on InstrumentationTestRunner.

There are nothing preventing JUnit4 tests from running on an Android device just like any other Java code. However, the Android test framework does some additional work:

  1. It sends test results back to your development machine over ADB.
  2. It does instrumentation (I'm not sure what exactly this involves).

The above is performed by some extensions specifically for JUnit3.

This JUnit3-specific code seems to be part of the InstrumentationTestRunner which forms part of the core Android system. However, as is evident with AndroidJUnitRunner, it is possible to use a custom test runner that supports JUnit4 or other frameworks.

Ralf
  • 14,655
  • 9
  • 48
  • 58
  • Is it possible to add "androidTestCompile" dependency in Android.mk file? if So can you please tell how? – Ambi Aug 08 '16 at 06:25
2

An explanation (in Japanese) is here:

http://d.hatena.ne.jp/esmasui/20120910/1347299594

I gather that the problem arises from the implementation of InstrumentationTestRunner.

The author's solution is the AndroidJUnit4 project.

usethe4ce
  • 23,261
  • 4
  • 30
  • 30