55

Tried out the suggestions on this post, but I still get the error:

!!! JUnit version 3.8 or later expected:

java.lang.RuntimeException: Stub!
    at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
    at junit.textui.TestRunner.<init>(TestRunner.java:54)
    at junit.textui.TestRunner.<init>(TestRunner.java:48)
    at junit.textui.TestRunner.<init>(TestRunner.java:41)

Process finished with exit code -3

Not sure what to do from here. Tried reorganizing the order of the Junit dependency. No luck, anyone else get this issue?

Android Studio V. 1.2.1.1

Community
  • 1
  • 1
Timothy Frisch
  • 2,995
  • 2
  • 30
  • 64

15 Answers15

40

Solved the issue. I was running the JUnit tests as a standard "JUnit test" in Android Studio. I fixed the issue by resetting the configuration as a Android Test.

Timothy Frisch
  • 2,995
  • 2
  • 30
  • 64
  • 4
    This worked for me but it took a bit to work out how to create an `Android Test`. Go to `Edit Configurations`, select `Android Tests` from the menu on the left, click `+`, and fill in the details on the right. – Bulwinkel Oct 24 '15 at 01:55
  • 5
    if you want to run your test as a standard Junit test be sure your build variant in android studio is set to Unit Tests and then create a Junit test configuration. – ericharlow Nov 17 '15 at 16:21
29

Just to add one more situation. I was having this error after updating Android Studio to 4.1 on a project using old AGP (3.3.2) and Gradle (4.10.2). What ended up solving the issue for me was removing the android.test.runner library from gradle dependencies, that is removing the following line from the app's module build.gradle:

useLibrary 'android.test.runner'

I probably got lucky to not actually need any of the classes provided by the library for unit or instrumented tests.

ianribas
  • 1,296
  • 10
  • 12
7

I solve this problem by moving "testCompile 'junit:junit:4.12'" to the first order in build.gradle dependency block

user3278144
  • 71
  • 1
  • 2
7

If you have updated android studio version to 4.1 wiuth latest gradle plygin. Please comment out

// useLibrary 'android.test.runner' It will fix error for you.

DJDeveloper
  • 91
  • 1
  • 4
7

This is apparently an Android Studio 4.1 issue. There's an open ticket for that already: https://issuetracker.google.com/issues/170328018

What really solved the issue for me was reinstalling the Android 11 (API 30) SDK, according to the bug ticket above:

"...there was an issue with API 30 mock jar that was built incorrectly. Can you open Android Studio SDK Manager and try uninstalling the R SDK or any Android SDK Platform 30 please? The Gradle build will auto install it when you try to rebuild after you delete the R SDK."

Henrique
  • 822
  • 8
  • 13
5

Running into this when opening an old project into latest Android Studio 2.3 after it auto-updated the project.

Android Studio supports "Android JUnit" tests which should run using the current host JRE and properly support JUnit 4+. The error is due to the wrong JRE being used.

Solved by using Edit Configuration > select the Android JUnit configuration > edit the JRE. Select any choice, use Apply, then change it back to "Default (Android Platform)" for it work.

ralfoide
  • 2,705
  • 4
  • 23
  • 21
5

The below fix is a work around and a repetitive task you need to perform on every new class that you create. I have used Gradle for my project on Android Studio.

Step 1: Go to Run-> Edit Configurations Option

Step 2: For each JUnit test configuration, change and add the below into the fields: a) JRE: Default is selected. Change it to the JRE version you have on your PC which you get as an option in the drop-down. As of today, its 1.8 b) Before launch field: Click on '+', select your module and add task as "clean compileTestJava". Move it to top in the list JUnit test configuration

Step 3: Apply Changes and now run your java class.

Swati Singh
  • 51
  • 1
  • 4
2

To solve this issue, I set "JRE" to "1.8". This is Under "Edit Configurations"

prijupaul
  • 2,076
  • 2
  • 15
  • 17
2

I was having the same error and I fixed it by updating JRE value under "Edit Configurations..." in Android Studio, I used API 28 instead and then it worked. Please check the image for your reference...

enter image description here

Alejandro Casanova
  • 3,633
  • 4
  • 31
  • 46
1

In my case I just forgot that I've changed the build variant to "release". Switching back to "debug" fixed everything. FML.

WindRider
  • 11,958
  • 6
  • 50
  • 57
1

Running an "Android JUNit" config and having JUnit 4.12 in gradle did not help to resolve this.

I needed to select JRE to Android API 27 Platform in the run config manually instead of "Default" to resolve the issue.

lubosz
  • 848
  • 10
  • 12
1

I had to update a Gradle settings file according to these instructions. I just added the line

testImplementation 'junit:junit:4.12'

to the dependencies block of my app's build.gradle file. If you check the "Project" tab in Android studio (where you can see your source code files) there should be a "Gradle Scripts" section. Look for the build.gradle file named after your package and add that line to the dependencies block, then click the "Sync now" prompt that should come up.

Jack M
  • 4,769
  • 6
  • 43
  • 67
0

I was also getting similar error when I added new project to workspace. May be they(new Project) would have upgraded Junit version or manadated that project should always use specific version.

In this cases, I closed project(all) along with original workspace and opened in new one.

0

I've managed to solve it with simply editing the project's app.iml and moving order entry after from bottom:

<orderEntry type="library" exported="" scope="TEST" name="junit-4.12" level="project" />

Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
0

In my case, the problem was the import, it should be :

import org.junit.Assert.assertEquals