Issue
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.
Solution
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'
}
}
}
Answered By - wogsland