30

I'm trying to implement a simple junit test using gradle and running into the following issue when I run gradle test:

:compileJava
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:1: error: package org.junit does not exist
import static org.junit.Assert.assertEquals;
                       ^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:1: error: static import only from classes and interfaces
import static org.junit.Assert.assertEquals;
^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:2: error: package org.junit does not exist
import org.junit.Test;
                ^
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:5: error: cannot find symbol
  @Test
   ^
  symbol:   class Test
  location: class CalculatorTest
/Users/wogsland/Projects/gradling/src/test/CalculatorTest.java:9: error: cannot find symbol
    assertEquals(6, sum);
    ^
  symbol:   method assertEquals(int,int)
  location: class CalculatorTest
5 errors
:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
Compilation failed; see the compiler error output for details.

So I've got this build.gradle file:

apply plugin: 'java'

dependencies {
    testCompile 'junit:junit:4.12'
}

sourceSets {
  main {
    java {
      srcDir 'src'
    }
  }
}

And CalculatorTest.java:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class CalculatorTest {
  @Test
  public void evaluatesExpression() {
    Calculator calculator = new Calculator();
    int sum = calculator.evaluate("1+2+3");
    assertEquals(6, sum);
  }
}

But I can't figure out why it's not finding junit when I've got it included in the dependencies.

6 Answers 6

54

So apparently I needed to add a compile dependency and then also declare repositories. My new build.gradle that successfully runs the test:

apply plugin: 'java'

repositories { jcenter() }
dependencies {
    testCompile 'junit:junit:4.12'
    compile 'junit:junit:4.12'
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
}
1
8

I have same issue in my latest android version and I have resolved it using below code. I hope you get help.

dependencies {
    testImplementation 'junit:junit:4.12'
    implementation 'junit:junit:4.12'
}
1

I know this is old, but I've run into this recently. You should be able to change the srcdirs for testing with gradle and also have unit tests work. If you want a src/* structure then just put all your tests in test/*.

The issue you're likely experiencing is that if you include your tests in your main/java code folder it will try to compile them during that phase. Move them outside of the src folder and update the srcdir structure accordingly and it should work as expected.

apply plugin: 'java'

repositories { jcenter() }
dependencies {
    testCompile 'junit:junit:4.12'
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
    test {
        java {
            srcDir 'test'
        }
    }
}
1

I really felt I had to promote @WillC's comment to a top-level answer because this is the key point. (I have marked this as community wiki as it's not fair for me to get any rep from this.)

See https://issuetracker.google.com/issues/37098629 : if you have changed sourceSets, gradle is broken for unit tests and you have to compile junit into the production APK.

If you specify sourceSets at all in your build.gradle, you will find that Gradle won't work with testImplementation (or testCompile) entries. It just totally ignores the dependencies and those classes can't be found.

The only workaround, as @WillC noted, is to compile the test dependencies into the production APK by changing testImplementation (and testCompile) to implementation (and compile). This is an anti-pattern (!!) but unfortunately the only way around this issue.

For me, I found that even though I didn't specify sourceSets, my IDE (Android Studio) "helpfully" added that section for me when I added a source tree, and this triggered the issue. As I don't need anything custom for sourceSets, I just removed it and this fixed the issue.

The issue is covered by a Priority 1 bug as linked in the quoted comment above, but was opened in 2016 and is still not resolved :'(

0

Try adding

 repositories {
    maven { url 'http://repo1.maven.org/maven2' }

directly under your buildscript{ in build.gradle

2
  • So, missing the buildscript { might be the issue?
    – wogsland
    Commented Mar 8, 2017 at 17:19
  • The above build.gradle is literally all I have.
    – wogsland
    Commented Mar 8, 2017 at 17:19
0

Add these depencies in your build.gradle file:

testImplementation 'junit:junit:4.12'
implementation 'junit:junit:4.12'

androidTestImplementation 'com.android.support:support-annotations:28.0.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'

This solved my problem. hopefully this will work for you too.

Not the answer you're looking for? Browse other questions tagged or ask your own question.