Android Studio 2.1: error: package org.junit does not exist
Solution 1
It looks like Gradle is not doing it's job.
Manually adding the jars fixed the problem.
Solution 2
I changed TestCompile to androidTestCompile and it's worked without problems.
testCompile 'junit:junit:4.12'
to
androidTestCompile 'junit:junit:4.12'
Solution 3
add this dependency to solve your issue
testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'
Solution 4
My tests are in src/test/java
folder and adding test.setRoot('test')
to sourceSets
worked for me.
sourceSets {
test.setRoot('test')
}
Solution 5
Some things you should check -
- Do you have unit test and debug selected under build variants?
- Is your working directory set to $MODULE_DIR$ in Run/Debug configurations for the unit test?
- Did you create the test by selecting the class you wish to test, going to Navigate -> Test and having Android Studio construct the test class for you?
Related videos on Youtube
Aggressor
I'm making a hardcore tactical rpg with epic loot called Grrbls. Follow along at www.Grrbls.com Current: -Senior Consultant at Avanade -Lead Designer & Programmer For Pixlyst Previous: -Lead iOS and Android Developer for PumpUp. -Programmer at Clipwire Games If you are interested in joining on to help with the development of an incredibly hardcore tactical RPG (something in between Disgaea and Final Fantasy Tactics) drop me an email at [email protected]
Updated on July 18, 2020Comments
-
Aggressor almost 4 years
Update: Its a bug and it's been reported, please star: https://code.google.com/p/android/issues/detail?id=209832&thanks=209832&ts=1463161330
I'm setting up unit testing on Android studio.
I have read the documentation and set it up exactly as specified. I have my test folder set up as
src/test/java
I've made a random test class:
import org.junit.Test; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; public class RandomTestClass { @Test public void testTest() { assertThat(4, is(4)); } }
However when I go to run my test I get:
error: package org.junit does not exist
I've set up my gradle EXACTLY as descibed in the docs:
dependencies { // Required -- JUnit 4 framework testCompile 'junit:junit:4.12' // Optional -- Mockito framework testCompile 'org.mockito:mockito-core:1.10.19' }
The few other questions with this issue seemed to say these dependencies are missing. I have them.
Can you think of any reason my Local Unit Tests are not finding the junit file when I go to run the test?
Note It's able to find the junit classes when Im writing the code. It only can't find junit when I run the test.
-
galaxigirl over 7 yearsDid you find a solution?
-
-
Aggressor about 8 yearsI don't know what you mean build variants. ` $MODULE_DIR$` is set as the working directory. Using the Navigate->Test to create the test had no effect but I tried it.
-
coder-don about 8 yearsHmm. Want to help but not sure this is something I could diagnose without seeing the actual code base (this seems like some sort of environmental issue). Maybe someone with more experience than me can chime in if they've seen this behavior before. RE: build variants, you can access the build variants menu in the bottom left part of the screen, just above favorites
-
Amir Uval over 7 yearstestCompile is for the
test
directory, androidTestCompile is for theandroidTest
one -
galaxigirl over 7 yearsI was very hopeful about this solution but it did not solve it for me
-
Aggressor over 7 yearsFor now use
compile
. It is the only other way it will work