Kotlin-dsl-samples: Use JUnit 5 for testing?

Created on 14 Sep 2017  路  9Comments  路  Source: gradle/kotlin-dsl-samples

Junit 5 has now released their GA release.

There's quite a few really nice features that may be nice to use with Junit 5. Including grouping tests by rules (ones that may fail under JDK 9 but work under JDK 8) ect.

Additionally, there will hopefully soon be kotlin specific assertions.

Would there be an interest in reviewing a PR that converted the build system to using the new Junit 5 test engine?

Most helpful comment

@hanct this issue is for converting the kotlin-dsl project itself to use JUnit 5.

However, here is a quick sample that might help push you in the right direction:

buildscript {
  repositories {
    mavenCentral()
    jcenter()
  }
  dependencies {
    classpath("org.junit.platform:junit-platform-gradle-plugin:1.0.1")
  }
}

plugins {
  `java`
}

apply {
  plugin("org.junit.platform.gradle.plugin")
}

extensions.getByType(JUnitPlatformExtension::class.java).apply {
  filters {
    engines {
      include("junit-jupiter")
    }
  }
  logManager = "org.apache.logging.log4j.jul.LogManager"
  details = Details.TREE
}

val junitPlatformVersion = "1.0.1"
val junitJupiterVersion  = "5.0.1"
val log4jVersion = "2.9.1"

dependencies {
  testImplementation("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
  testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")

  // To use Log4J"s LogManager
  testRuntimeOnly("org.apache.logging.log4j:log4j-core:${log4jVersion}")
  testRuntimeOnly("org.apache.logging.log4j:log4j-jul:${log4jVersion}")
}

All 9 comments

Would there be an interest in reviewing a PR that converted the build system to using the new Junit 5 test engine?

Yes.

So I'm realizing that some of this code is pretty intertwined with how Junit 4's rules worked. For example:

    @JvmField
    @Rule val testName = TestName()

    @JvmField
    @Rule val temporaryFolder = TemporaryFolder()

I'm wondering if it would make sense to, as a first pass, use junit5's vintage support for backwards compatibility, them moving forward, try to get more tests to use Junit 5.

Thoughts? How do you want to do this?

  • I could attempt a refactor of the tests to use the new Junit 5 extensions. (May have a larger impact and may take longer).
  • I could just port the tests that don't rely upon the @Rule annotations. (Some tests will still use the Junit 4 annotations, some tests won't. All can use the new Junit 5 assertions).

These two rules TestName and TemporaryFolder are the only ones we use for now.
Should be pretty easy to port them to JUnit5 extensions, or use similar existing ones if any.

I'm a bit concerned about some missing features of the JUnit 5 support though. For example I would 馃憥 the migration if at this point we loose the Gradle HTML test reports or proper test reporting in Build Scans.

Is there a way to wire in the Junit XML output into the buildscan configuration?

I could get HTML test report generation using this method:
https://stackoverflow.com/a/39455463/3708426

Is there any sample code which i can refer in order to use junit 5 in kotlin-dsl ?

@hanct this issue is for converting the kotlin-dsl project itself to use JUnit 5.

However, here is a quick sample that might help push you in the right direction:

buildscript {
  repositories {
    mavenCentral()
    jcenter()
  }
  dependencies {
    classpath("org.junit.platform:junit-platform-gradle-plugin:1.0.1")
  }
}

plugins {
  `java`
}

apply {
  plugin("org.junit.platform.gradle.plugin")
}

extensions.getByType(JUnitPlatformExtension::class.java).apply {
  filters {
    engines {
      include("junit-jupiter")
    }
  }
  logManager = "org.apache.logging.log4j.jul.LogManager"
  details = Details.TREE
}

val junitPlatformVersion = "1.0.1"
val junitJupiterVersion  = "5.0.1"
val log4jVersion = "2.9.1"

dependencies {
  testImplementation("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
  testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")

  // To use Log4J"s LogManager
  testRuntimeOnly("org.apache.logging.log4j:log4j-core:${log4jVersion}")
  testRuntimeOnly("org.apache.logging.log4j:log4j-jul:${log4jVersion}")
}

Now that Gradle core supports JUnit 5 natively, this is probably in a good position to move forward.

Is there any update on this? Has it been implemented yet?

The Gradle Kotlin DSL tests now rely on the test support and fixtures from gradle/gradle. They won't be migrated to JUnit 5 in a reasonable time frame, closing for now.

Was this page helpful?
0 / 5 - 0 ratings