1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 16:28:12 -07:00

JUnit: eliminate classes with no @Test annotations.

Otherwise, JUnit 4 throws :-(
This commit is contained in:
Cedric Beust 2015-12-26 10:24:47 +04:00
parent f34a220ff4
commit ddbe2e376e
2 changed files with 17 additions and 4 deletions

View file

@ -25,7 +25,8 @@ abstract class GenericTestRunner : ITestRunnerContributor {
if (project.testDependencies.any { it.id.contains(dependencyName)}) IAffinity.DEFAULT_POSITIVE_AFFINITY if (project.testDependencies.any { it.id.contains(dependencyName)}) IAffinity.DEFAULT_POSITIVE_AFFINITY
else 0 else 0
protected fun findTestClasses(project: Project, classpath: List<IClasspathDependency>): List<String> { protected fun findTestClasses(project: Project, classpath: List<IClasspathDependency>,
classFilter : (Class<*>) -> Boolean = {true}): List<String> {
val path = KFiles.joinDir(project.directory, project.buildDirectory, KFiles.TEST_CLASSES_DIR) val path = KFiles.joinDir(project.directory, project.buildDirectory, KFiles.TEST_CLASSES_DIR)
val result = KFiles.findRecursively(File(path), arrayListOf(File(".")), { val result = KFiles.findRecursively(File(path), arrayListOf(File(".")), {
file -> file.endsWith(".class") file -> file.endsWith(".class")
@ -36,8 +37,11 @@ abstract class GenericTestRunner : ITestRunnerContributor {
// Only keep classes with a parameterless constructor // Only keep classes with a parameterless constructor
val urls = arrayOf(File(path).toURI().toURL()) + val urls = arrayOf(File(path).toURI().toURL()) +
classpath.map { it.jarFile.get().toURI().toURL() } classpath.map { it.jarFile.get().toURI().toURL() }
URLClassLoader(urls).loadClass(it).getConstructor() val cl = URLClassLoader(urls).loadClass(it)
true val constructor = cl.getConstructor()
// If we get past this, we have a default constructor
classFilter(cl)
} catch(ex: Exception) { } catch(ex: Exception) {
log(2, "Skipping non test class $it: ${ex.message}") log(2, "Skipping non test class $it: ${ex.message}")
false false

View file

@ -9,6 +9,15 @@ open public class JUnitRunner() : GenericTestRunner() {
override val dependencyName = "junit" override val dependencyName = "junit"
override fun args(project: Project, classpath: List<IClasspathDependency>) = findTestClasses(project, classpath) override fun args(project: Project, classpath: List<IClasspathDependency>)
= 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
}
} }