Skip to content

Commit

Permalink
Add basic support for JUnit 5
Browse files Browse the repository at this point in the history
#35

This is missing the ability to mix and match parameters
supported by Burst with parameters supported by JUnit 5.
  • Loading branch information
squarejesse committed Nov 5, 2024
1 parent 30ad9bc commit 81a4a5c
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,25 @@ class BurstGradlePluginTest {
}
}

@Test
fun junit5() {
val projectDir = File("src/test/projects/junit5")

val taskName = ":lib:test"
val result = createRunner(projectDir, "clean", taskName).build()
assertThat(result.task(taskName)!!.outcome).isIn(*SUCCESS_OUTCOMES)

val testResults = projectDir.resolve("lib/build/test-results")

with(readTestSuite(testResults.resolve("test/TEST-CoffeeTest_Regular.xml"))) {
assertThat(testCases.map { it.name }).containsExactlyInAnyOrder(
"test_Milk()",
"test_None()",
"test_Oat()",
)
}
}

private fun createRunner(
projectDir: File,
vararg taskNames: String,
Expand Down
41 changes: 41 additions & 0 deletions burst-gradle-plugin/src/test/projects/junit5/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

buildscript {
repositories {
maven {
url = file("$rootDir/../../../../../build/testMaven").toURI()
}
mavenCentral()
google()
}
dependencies {
classpath("app.cash.burst:burst-gradle-plugin:${project.property("burstVersion")}")
classpath(libs.kotlin.gradlePlugin)
}
}

allprojects {
repositories {
maven {
url = file("$rootDir/../../../../../build/testMaven").toURI()
}
mavenCentral()
google()
}

tasks.withType(JavaCompile::class.java).configureEach {
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
}

tasks.withType(KotlinJvmCompile::class.java).configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
}
}

tasks.withType<Test> {
useJUnitPlatform()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
kotlin("jvm")
id("app.cash.burst")
}

dependencies {
testImplementation(kotlin("test"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import app.cash.burst.Burst
import kotlin.test.BeforeTest
import kotlin.test.Test

@Burst
class CoffeeTest(
private val espresso: Espresso,
) {
@BeforeTest
fun setUp() {
println("set up $espresso")
}

@Test
fun test(dairy: Dairy) {
println("running $espresso $dairy")
}
}

enum class Espresso { Decaf, Regular, Double }

enum class Dairy { None, Milk, Oat }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
from(files("../../../../../gradle/libs.versions.toml"))
}
}
}

include(":lib")
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ internal class BurstApis private constructor(
return BurstApis(pluginContext, junitPackage)
}

if (pluginContext.referenceClass(junit5TestClassId) != null) {
return BurstApis(pluginContext, junit5Package)
}

if (pluginContext.referenceClass(kotlinTestClassId) != null) {
return BurstApis(pluginContext, kotlinTestPackage)
}
Expand All @@ -56,11 +60,15 @@ private val burstValuesId = burstFqPackage.callableId("burstValues")

private val junitPackage = FqPackageName("org.junit")
private val junitTestClassId = junitPackage.classId("Test")
private val junit5Package = FqPackageName("org.junit.jupiter.api")
private val junit5TestClassId = junit5Package.classId("Test")
private val kotlinTestPackage = FqPackageName("kotlin.test")
private val kotlinTestClassId = kotlinTestPackage.classId("Test")

internal val IrAnnotationContainer.hasAtTest: Boolean
get() = hasAnnotation(junitTestClassId) || hasAnnotation(kotlinTestClassId)
get() = hasAnnotation(junitTestClassId) ||
hasAnnotation(junit5TestClassId) ||
hasAnnotation(kotlinTestClassId)

internal val IrAnnotationContainer.hasAtBurst: Boolean
get() = hasAnnotation(burstAnnotationId)

0 comments on commit 81a4a5c

Please sign in to comment.