25
votes

My Android studio project has an app module which is android framework dependent, I have created a new module called domain and it contains only pure java classes and a few test classes.

In this module's build.gradle file, I have added junit and mockito libraries for testing purpose as follows:

apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
    testCompile 'junit:junit:4.11'
    testCompile 'org.mockito:mockito-core:1.9.5'
    compile project(':common')
    compile project(':model')
    //test dependencies
}

But Android Studio is giving me !!! JUnit version 3.8 or later expected error message whenever I try to execute the test class.

All the suggested solutions to this problem is to open the Project Structure | Modules | Dependencies, and move the junit-4.7.jar up, so that it comes before Android 1.6 Platform in the classpath.

In fact, I have tried to implement this solution but the problem still exists. Image of my dependencies structure

Any idea on how to resolve this issue?

4
What does your Run/Debug Configuration look like for your tests? Specifically, what does the property "Use classpath of module" say for your tests? You can find this on Run -> Edit Configurations, then select a configuration on the left.Patrick McLaren
Post your build.gradle.Jared Burrows
I could solve it by removing unitTest.returnDefaultValues, check: stackoverflow.com/a/44820309/965569Chiara

4 Answers

50
votes

I've already solved this just now. It may help you... maybe. A little instruction:

  1. Go to Run -> Edit Configurations
  2. Delete JUnit configuration on left panel

enter image description here

8
votes

I've managed to solve it with simply editing the project's iml and moving order enrty for junit , in my case:

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

up to be the first orderEntry after </content>

0
votes

I had this but the reason was different..I had inadvertently set my build variant to Release and didn't have any signing info specified so nothing could be built and run on the device.

The cryptic error it gave me was this same one when I tried running the integration tests...didn't notice the app was also disabled :-P Set it back to Debug and it worked fine.

-7
votes

I managed to solve the problem by by changing the following in .iml file

from:

 <component name="NewModuleRootManager" inherit-compiler-output="true">

to:

  <orderEntry type="inheritedJdk" />