23

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: 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.

Aggressor
  • 13,323
  • 24
  • 103
  • 182

7 Answers7

31

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

testCompile 'junit:junit:4.12'

to

androidTestCompile 'junit:junit:4.12'
Amir Uval
  • 14,425
  • 4
  • 50
  • 74
Caner Balım
  • 556
  • 9
  • 21
5

add this dependency to solve your issue

testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'
Tech ADR
  • 51
  • 1
  • 1
2

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?
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
coder-don
  • 113
  • 7
  • I 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. – Aggressor May 05 '16 at 23:52
  • Hmm. 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 – coder-don May 05 '16 at 23:59
2

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

Manually adding the jars fixed the problem.

Aggressor
  • 13,323
  • 24
  • 103
  • 182
2

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

sourceSets {
    test.setRoot('test')
}
Gokhan Arik
  • 2,626
  • 2
  • 24
  • 50
0

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'.

Alok C
  • 2,787
  • 3
  • 25
  • 44
Or Harel
  • 525
  • 1
  • 6
  • 13
-1

Download (junit-4.12.jar) from https://github.com/junit-team/junit4/wiki/Download-and-Install 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.

ykaragol
  • 6,139
  • 3
  • 29
  • 56
hassan
  • 19
  • 2