[Solved] Android Studio 2.1: error: package org.junit does not exist

Update: Its a bug and it’s been reported, please star:

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:
enter image description here

import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

public class RandomTestClass
    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?

It’s able to find the junit classes when Im writing the code. It only can’t find junit when I run the test.

Solution #1:

I changed TestCompile to androidTestCompile and it’s worked without problems.

testCompile 'junit:junit:4.12'


androidTestCompile 'junit:junit:4.12'
Respondent: Aggressor

Solution #2:

It looks like Gradle is not doing it’s job.

Manually adding the jars fixed the problem.

Respondent: Caner Bal?m

Solution #3:

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?
Respondent: Aggressor

Solution #4:

My tests are in src/test/java folder and adding test.setRoot('test') to sourceSets worked for me.

sourceSets {
Respondent: coder-don

Solution #5:

add this dependency to solve your issue

testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'
Respondent: Gokhan Arik

Solution #6:

Try to change the Build Variant to debug.

View -> Tool Windows -> Build Variants

UPD: This actually may be the cause, when all gradle dependencies are met, but org.junit.Test still not found. At least it was a cause for me. It was not working in ‘release’ build type, but was working in ‘debug’.

Respondent: Tech ADR

Solution #7:

Download (junit-4.12.jar) from
and copy it in your libs folder:(yourProjectFolder/app/libs)

and then in build.gradle(Module: app) use this codes:

dependencies {
compile files('libs/junit-4.12.jar')

then rebuild your project.

Respondent: Or Harel

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy