Gradle compileJava error: org.junit package does not exist

I am trying to run a simple junit test using gradle and ran into the following problem when running 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 have 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 understand why it does not find junit when I turned it on depending.

+21
junit build.gradle gradle
source share
4 answers

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

 apply plugin: 'java' repositories { jcenter() } dependencies { testCompile 'junit:junit:4.12' compile 'junit:junit:4.12' } sourceSets { main { java { srcDir 'src' } } } 
+41
source share

I have the same problem in my latest version of Android, and I solved it using the code below. I hope you get help.

 dependencies { testImplementation 'junit:junit:4.12' implementation 'junit:junit:4.12' } 
+1
source share

Try to add

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

directly under your buildscript {in build.gradle

0
source share

I know this is old, but I came across this recently. You should be able to modify srcdirs to test with gradle, and also run unit tests. If you need an src / * structure, just put all your tests in test / *.

The problem that you are likely to encounter is that if you include your tests in the main / java code folder, they will try to compile them at this point. Move them outside 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' } } } 
0
source share

All Articles