diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt index e7b4756b..0cf3eb89 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt @@ -25,7 +25,8 @@ abstract class GenericTestRunner : ITestRunnerContributor { if (project.testDependencies.any { it.id.contains(dependencyName)}) IAffinity.DEFAULT_POSITIVE_AFFINITY else 0 - protected fun findTestClasses(project: Project, classpath: List): List { + protected fun findTestClasses(project: Project, classpath: List, + classFilter : (Class<*>) -> Boolean = {true}): List { val path = KFiles.joinDir(project.directory, project.buildDirectory, KFiles.TEST_CLASSES_DIR) val result = KFiles.findRecursively(File(path), arrayListOf(File(".")), { file -> file.endsWith(".class") @@ -36,8 +37,11 @@ abstract class GenericTestRunner : ITestRunnerContributor { // Only keep classes with a parameterless constructor val urls = arrayOf(File(path).toURI().toURL()) + classpath.map { it.jarFile.get().toURI().toURL() } - URLClassLoader(urls).loadClass(it).getConstructor() - true + val cl = URLClassLoader(urls).loadClass(it) + val constructor = cl.getConstructor() + // If we get past this, we have a default constructor + + classFilter(cl) } catch(ex: Exception) { log(2, "Skipping non test class $it: ${ex.message}") false diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JUnitRunner.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JUnitRunner.kt index 64350134..bf142419 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JUnitRunner.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JUnitRunner.kt @@ -9,6 +9,15 @@ open public class JUnitRunner() : GenericTestRunner() { override val dependencyName = "junit" - override fun args(project: Project, classpath: List) = findTestClasses(project, classpath) + override fun args(project: Project, classpath: List) + = findTestClasses(project, classpath) { + // Only return a class if it contains at least one @Test method, otherwise + // JUnit 4 throws an exception :-( + it.declaredMethods.flatMap { + it.annotations.toList() + }.filter { + ann: Annotation -> ann.javaClass.name.contains("Test") + }.size > 0 + } }