This was indeed quite simple. I modified the snippet above as follows:
task('test' + f, type: Test) {
include fileTree(t).collect {
'**/' + it.name.replaceAll("\\.java", "\\.class")
}
}
which gives me the desired result without any additional configuration.