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

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.

  • Clicking on Notification is not starting intended activity?
  • Android SDK not installing on win 7 64-bit
  • Where can I find google-play-services_lib.jar?
  • Create Android App That Acts As A Shortcut To Our Mobile Site
  • Can I draw rectangle in XML?
  • Sync Android devices via GPS time?
  • 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
    {
        @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.

  • Bring application to front after user clicks on home button
  • Can I use Android's AccountManager for getting OAuth access token for AppEngine?
  • Prevent body from scrolling (mobile, native browsers)
  • Android grow heap frag case
  • Strange Lint Warning - Unexpected text found in layout file: “”
  • AppBarLayout with FrameLayout container as scrolling content doesn't work
  • 4 Solutions collect form web for “Android Studio 2.1: error: package org.junit does not exist”

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

    testCompile 'junit:junit:4.12'
    

    to

    androidTestCompile 'junit:junit:4.12'
    

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

    Manually adding the jars fixed the problem.

    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?

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

    sourceSets {
        test.setRoot('test')
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.