Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is JUnit 4 on Android not working?

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?

like image 563
Matthew Avatar asked Mar 21 '12 16:03

Matthew


People also ask

Is JUnit 4 better than JUnit 5?

Only one test runner can execute tests at a time in JUnit 4 (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously. JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of the Java 8 features.

Can JUnit 4 and 5 be used together?

Luckily, JUnit 4 and JUnit 5 features can coexist, even within the same test classes. All we need to do is add the necessary JUnit 5 dependencies to our dependencies file. For example, below is a snippet of a Maven POM file.

What is the difference between JUnit 4 and 5?

Differences Between JUnit 4 and JUnit 5 Some other differences include: The minimum JDK for JUnit 4 was JDK 5, while JUnit 5 requires at least JDK 8. The @Before , @BeforeClass , @After , and @AfterClass annotations are now the more readable as the @BeforeEach , @BeforeAll , @AfterEach , and @AfterAll annotations.

What is difference between JUnit 3 and JUnit 4?

JDK required. JUnit 4 uses a lot from Java 5 annotations, generics, and static import features. Although the JUnit 3. x version can work with JDK 1.2+, this usage requires that the new version of JUnit be used with Java 5 or higher.


2 Answers

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.

like image 141
Ralf Avatar answered Oct 04 '22 06:10

Ralf


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.

like image 26
usethe4ce Avatar answered Oct 04 '22 06:10

usethe4ce