25

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?

buczek
  • 2,011
  • 7
  • 29
  • 40
Fshamri
  • 1,346
  • 4
  • 17
  • 32
  • 1
    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 Mar 20 '15 at 17:47
  • Post your `build.gradle`. – Jared Burrows Mar 20 '15 at 18:09
  • I could solve it by removing `unitTest.returnDefaultValues`, check: https://stackoverflow.com/a/44820309/965569 – Chiara Jun 29 '17 at 08:48

4 Answers4

50

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

voronnenok
  • 712
  • 6
  • 6
8

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>

Rotem Slootzky
  • 688
  • 10
  • 21
0

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.

kenyee
  • 2,309
  • 1
  • 24
  • 32
-7

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" />
Fshamri
  • 1,346
  • 4
  • 17
  • 32
  • This doesn't make sense. These are different tags. goes inside . – elliptic1 Apr 20 '15 at 13:34
  • This is not a good practice as the end-user of your project can import your project and Intellij will recreate the *.iml file. – Kevin Crain Nov 08 '16 at 17:39
  • Found on my side that the problem is cached references for older dependencies and in this case this solution is leading other fellow developers to anti-patterns, this is especially bad if the developer starts his journey following posts like this. Do not modify the *.iml, its somewhere in the official Intellij documentation – Kevin Crain Nov 09 '16 at 10:38