Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting one simple Junit test to compile with Gradle on MacOSx

Tags:

java

junit

gradle

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. 
like image 863
Uncle Troy Avatar asked Jan 28 '13 03:01

Uncle Troy


People also ask

How do I run a single unit test in Gradle?

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.

How do I run JUnit in Gradle?

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.


2 Answers

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' } 
like image 189
Alpine Avatar answered Sep 22 '22 07:09

Alpine


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'

like image 33
ericharlow Avatar answered Sep 22 '22 07:09

ericharlow