28
votes

I'm trying to get the most bare-bones gradle project (with tests) to build. Have looked at all related questions and Google searches, and I seem to be missing something very fundamental, and apparently uncommon.

I created one test class, and "gradle compileTestJava" fails to compile the file saying

package org.junit does not exist

It finds the test, knows it's a test, but can't seem to find its own junit.jar file.

build.gradle contains

apply plugin: 'java'

and that is it. Bare bones! I also tried adding

dependencies {

testCompile 'junit:junit:4.10'

}

With that I get "Could not resolve all dependencies" which makes me think gradle has lost its way around its own files(?). I see gradle's installed /Users/me/Documents/Projects/gradle-1.3/lib/plugins/junit-4.10.jar file.

In fact, when I run "gradle dependencies" I get

testCompile - Classpath for compiling the test sources.
No dependencies

I have no idea if that is supposed to include built-in plugin dependencies or not. My guess is that it should list junit.

Any ideas?

Here's what I get:

:compileTestJava

/Users/me/Documents/Projects/experiment1/src/test/java/MyUnitTests.java:3: package org.junit does not exist
import org.junit.*;
       ^
/Users/me/Documents/Projects/experiment1/src/test/java/MyUnitTests.java:7: cannot find symbol
symbol  : class Test
location: class test.java.MyUnitTests
    @Test
     ^
/Users/me/Documents/Projects/experiment1/src/test/java/MyUnitTests.java:9: cannot find symbol
symbol  : variable Assert
location: class test.java.MyUnitTests
            Assert.assertEquals(2 + 2, 4);
            ^
3 errors
 FAILED

FAILURE: Build failed with an exception.
6
Did you find solution to this problem? I am stuck here and every solution found did not work so far. I am trying to run unit tests in my android SDK project and facing same problem.user484691

6 Answers

16
votes

You need to make sure you have your repos set up, and you need to include the dependency for junit. This means that you need something that looks like this for your build.gradle

repositories {
    mavenCentral()
}

apply plugin: 'java'

dependencies {
    testCompile 'junit:junit:4.10'
}
6
votes

In my build.gradle I had this:

android {
sourceSets { main { java.srcDirs = ['src/main/java', 'src/test/java'] } }

And also

dependencies {
testCompile 'junit:junit:4.12'}

The problem went away when I deleted the following from the build.gradle

'src/test/java'

4
votes

This guide might help - http://www.slideshare.net/tobiaspreuss/how-to-setup-unit-testing-in-android-studio

Latest gradle the test should be under androidTest dir

Also in your gradle.build:

dependencies {
     androidTestCompile 'junit:junit:4.+'
}

also add those under defaultConfig

defaultConfig {

    testPackageName "test.java.foo"
    testInstrumentationRunner "android.test.InstrumentationTestRunner"

}
2
votes

It might also need mavenLocal() in your repositories declaration

repositories {
  mavenLocal()
  mavenCentral()
}
0
votes

Just add

repositories {
    mavenCentral()
}

It worked for me, if you are using Android Studio for Android development

0
votes

I had a similar kind of issue on my testcases and got similar kind of error. I am able to fix it by adding the source sets in my build.gradle file.

For example:

sourceSets {
src {
   test{
      java{
    srcDirs =["com/testfile/appservice/test/endpoint"]
    }
      }
}

}

src/test/java is the name of the directory just above the test package and com.testfile.appservice.test.endpoint is the name of package where my junit test is written.