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.
You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.
In your Gradle project, in the editor, create or select a test to run. From the context menu, select Run <test name>. icon in the left gutter. If you selected the Choose per test option, IntelliJ IDEA displays both Gradle and JUnit test runners for each test in the editor.
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' }
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With