Why does the test task ignore classes I added to the testRuntime classpath?

Please help me,

I want to automate testing my students’ turnins. Therefore I copy a build.gradle file into each turnin directory and run a build. Since I have a set of compiled JUnit tests in another project, I added the directory with the .class files to the testRuntime classpath. Unfortunately the test task ignores the classes and executes 0 tests. What’s my mistake?

//This build file will be used on each turnin
  apply plugin: 'java'
apply plugin: 'eclipse'
  repositories {
 mavenCentral()
}
  dependencies {
 testCompile 'junit:junit:4.10'
 testCompile 'org.hamcrest:hamcrest-all:1.1'
 testRuntime files('../../turnin-tests/build/classes/test')
}
  sourceSets {
 main {
  java {
   srcDir '.'
  }
 }
}
  test.doFirst {
 println "${project.sourceSets.test.runtimeClasspath.files.join('\n')}"
}
mak:00864359 klaus$ pwd
/Users/klaus/Documents/dritte/hm/2011-se1/turnin-explode/00864359
mak:00864359 klaus$ ll
total 48
drwxr-xr-x@
7 klaus
staff
 238 30 Nov 08:39 .
drwxr-xr-x
35 klaus
staff
1190 30 Nov 08:20 ..
-rw-r--r--
 1 klaus
staff
4951 30 Nov 06:57 ICEControlCenter.java
-rw-r--r--
 1 klaus
staff
 931 30 Nov 06:21 SlidingWindow.java
-rw-r--r--
 1 klaus
staff
1465 30 Nov 06:49 Wagon.java
-rw-r--r--
 1 klaus
staff
1721 30 Nov 06:21 Worm.java
-rw-r--r--
 1 klaus
staff
 425 30 Nov 08:28 build.gradle
mak:00864359 klaus$ ll ../../turnin-tests/build/classes/test
total 56
drwxr-xr-x
8 klaus
staff
 272 30 Nov 08:10 .
drwxr-xr-x
4 klaus
staff
 136 30 Nov 08:10 ..
-rw-r--r--
1 klaus
staff
5891 30 Nov 08:10 ICEControllerCenterTestBasic.class
-rw-r--r--
1 klaus
staff
2194 30 Nov 08:10 ICEControllerCenterTestFreeTracks.class
-rw-r--r--
1 klaus
staff
1785 30 Nov 08:10 ICEControllerCenterTestShorten.class
-rw-r--r--
1 klaus
staff
2916 30 Nov 08:10 SlidingWindowTest.class
-rw-r--r--
1 klaus
staff
2105 30 Nov 08:10 WagonTest.class
-rw-r--r--
1 klaus
staff
3861 30 Nov 08:10 WormTest.class
mak:00864359 klaus$ gradle test
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
/Users/klaus/Documents/dritte/hm/2011-se1/turnin-explode/00864359/build/classes/test
/Users/klaus/Documents/dritte/hm/2011-se1/turnin-explode/00864359/build/resources/test
/Users/klaus/Documents/dritte/hm/2011-se1/turnin-explode/00864359/build/classes/main
/Users/klaus/Documents/dritte/hm/2011-se1/turnin-explode/00864359/build/resources/main
/Users/klaus/Documents/dritte/hm/2011-se1/turnin-tests/build/classes/test
/Users/klaus/.gradle/caches/artifacts-4/junit/junit/c12498cf18507aa6433a94eb7d3e77d5/jars/junit-4.10.jar
/Users/klaus/.gradle/caches/artifacts-4/org.hamcrest/hamcrest-all/c12498cf18507aa6433a94eb7d3e77d5/jars/hamcrest-all-1.1.jar
/Users/klaus/.gradle/caches/artifacts-4/org.hamcrest/hamcrest-core/c12498cf18507aa6433a94eb7d3e77d5/jars/hamcrest-core-1.1.jar

The directory build/test-results is empty, and the test report html file says 0 tests were executed. :-?

To add the contents of a directory to a configuration, you need to use ‘fileTree()’, not ‘files()’. The latter just gives a ‘flat’ collection of one or more File’s.

Hi Peter, a Thanks for your answer. Unfortunately replacing ‘files()’ with ‘fileTree()’ doesn’t solve my issue. Actually ‘project.sourceSets.test.runtimeClasspath’ looked better before, since it contained the directory.

Any further ideas?

Regards, Klaus

Hi Peter, a Thanks for your answer. Unfortunately replacing ‘files()’ with ‘fileTree()’ doesn’t solve my issue. Actually ‘project.sourceSets.test.runtimeClasspath’ looked better before, since it contained the directory.

Any further ideas?

Regards, Klaus

Have you checked that the file tree that you construct contains the expected classes?

I found it!

The test task’s property ‘testClassesDir’ was the solution:

//This build file will be used on each turnin
  apply plugin: 'java'
apply plugin: 'eclipse'
  repositories {
 mavenCentral()
}
  dependencies {
 testCompile 'junit:junit:4.10'
 testCompile 'org.hamcrest:hamcrest-all:1.1'
}
  sourceSets {
 main {
  java {
   srcDir '.'
  }
 }
}
  test {
 testClassesDir file('../../turnin-tests/build/classes/test')
}

Yes, you are right. Adding the directory itself was the right thing to do. From what I can see in the source code, Gradle only looks for test classes in build/classes/test (more precisely test.testClassesDir). So your best chance may be to copy your test classes there with a Copy task. Or you turn things around, always execute the build which contains the tests but pull in the code to be tested from different places.

If the project itself doesn’t contain any tests, that’s a good solution.