Why can't my simplest one-line build.gradle compile tests with JUnit jar?

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.

Other info: * I’m on MacOSx Mountain Lion * I did unzip using the default Finder * I’m assuming that I’ve checked file permissions and paths (gradle -v runs fine) * I’ve set up symlinks to avoid paths with whitespace

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.

Lastly, my file structure:

total 16
drwxr-xr-x
 5 me
staff
 170 Jan 20 20:08 .
drwxr-xr-x
15 me
staff
 510 Jan 27 19:29 ..
-rw-r--r--@
1 me
staff
6148 Jan 20 20:08 .DS_Store
drwxr-xr-x
 3 me
staff
 102 Jan 20 20:08 main
drwxr-xr-x
 4 me
staff
 136 Jan 20 20:09 test
  src/main:
total 0
drwxr-xr-x
3 me
staff
102 Jan 20 20:08 .
drwxr-xr-x
5 me
staff
170 Jan 20 20:08 ..
drwxr-xr-x
2 me
staff
 68 Jan 20 20:08 java
  src/main/java:
total 0
drwxr-xr-x
2 me
staff
 68 Jan 20 20:08 .
drwxr-xr-x
3 me
staff
102 Jan 20 20:08 ..
  src/test:
total 16
drwxr-xr-x
4 me
staff
 136 Jan 20 20:09 .
drwxr-xr-x
5 me
staff
 170 Jan 20 20:08 ..
-rw-r--r--@ 1 me
staff
6148 Jan 20 20:09 .DS_Store
drwxr-xr-x
3 me
staff
 102 Jan 27 19:06 java
  src/test/java:
total 8
drwxr-xr-x
3 me
staff
102 Jan 27 19:06 .
drwxr-xr-x
4 me
staff
136 Jan 20 20:09 ..
-rw-r--r--
1 me
staff
146 Jan 27 19:06 MyUnitTest.java

Thanks!

Rob

Your problem is that your code depends on ‘junit’, and your build has not configured it as a dependency. Gradle does not make ‘junit’ (or any other 3rd party library) available by default. The jar files under ‘lib’ are part of the Gradle implementation, and are not part of your project.

You need to choose which version of JUnit to use and explicitly add it as a dependency. You were on the right track with adding the ‘dependencies’ block to your build. The final piece missing is a ‘repositories’ block to tell Gradle where to find the declared dependencies.

I’d highly recommend that you check out the Gradle User Guide, which gives a step-by-step tutorial on getting started with a simple java project.

? He specifically does add it as a dependency for testCompile.

dependencies {

testCompile ‘junit:junit:4.10’ }

Why is this not valid?

The OP tried to add a ‘dependencies’ block after the “bare bones” approach didn’t work, but apparently he was still missing a ‘repositories’ block.

I had this problem because my test file was in the /java dir not in /test dir.