Skip to content

Commit 1a343a1

Browse files
Support JUnit 5 in @InterceptTest (#177)
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
1 parent 4510679 commit 1a343a1

File tree

9 files changed

+202
-0
lines changed

9 files changed

+202
-0
lines changed

burst-gradle-plugin/src/test/kotlin/app/cash/burst/gradle/TestInterceptorGradlePluginTest.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,61 @@ class TestInterceptorGradlePluginTest {
154154
)
155155
}
156156
}
157+
158+
@Test
159+
fun junit5KotlinTest() {
160+
val tester = GradleTester("interceptorJunit5")
161+
tester.cleanAndBuild(":lib:test")
162+
163+
with(tester.readTestSuite("app.cash.burst.tests.KotlinTestTest")) {
164+
assertThat(systemOut).isEqualTo(
165+
"""
166+
|intercepting kotlinTestTest
167+
|@kotlin.test.BeforeTest before test
168+
|@kotlin.test.Test running
169+
|@kotlin.test.AfterTest after test
170+
|intercepted kotlinTestTest
171+
|
172+
""".trimMargin(),
173+
)
174+
}
175+
}
176+
177+
@Test
178+
fun junit5OrgJunitTest() {
179+
val tester = GradleTester("interceptorJunit5")
180+
tester.cleanAndBuild(":lib:test")
181+
182+
with(tester.readTestSuite("app.cash.burst.tests.OrgJunitTest")) {
183+
assertThat(systemOut).isEqualTo(
184+
"""
185+
|intercepting orgJunitTest
186+
|@org.junit.Before before test
187+
|@org.junit.Test running
188+
|@org.junit.After after test
189+
|intercepted orgJunitTest
190+
|
191+
""".trimMargin(),
192+
)
193+
}
194+
}
195+
196+
@Test
197+
fun junit5OrgJunitJupiterApiTest() {
198+
val tester = GradleTester("interceptorJunit5")
199+
tester.cleanAndBuild(":lib:test")
200+
201+
with(tester.readTestSuite("app.cash.burst.tests.OrgJunitJupiterApiTest")) {
202+
assertThat(systemOut).isEqualTo(
203+
"""
204+
|intercepting orgJunitJupiterApiTest
205+
|@org.junit.jupiter.api.BeforeEach before test
206+
|@org.junit.jupiter.api.Test running
207+
|@org.junit.jupiter.api.AfterEach after test
208+
|intercepted orgJunitJupiterApiTest
209+
|
210+
""".trimMargin(),
211+
)
212+
}
213+
}
157214
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
2+
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
3+
4+
buildscript {
5+
repositories {
6+
maven {
7+
url = file("$rootDir/../../../../../build/testMaven").toURI()
8+
}
9+
mavenCentral()
10+
google()
11+
}
12+
dependencies {
13+
classpath("app.cash.burst:burst-gradle-plugin:${project.property("burstVersion")}")
14+
classpath(libs.kotlin.gradlePlugin)
15+
}
16+
}
17+
18+
allprojects {
19+
repositories {
20+
maven {
21+
url = file("$rootDir/../../../../../build/testMaven").toURI()
22+
}
23+
mavenCentral()
24+
google()
25+
}
26+
27+
tasks.withType(JavaCompile::class.java).configureEach {
28+
sourceCompatibility = "1.8"
29+
targetCompatibility = "1.8"
30+
}
31+
32+
tasks.withType(KotlinJvmCompile::class.java).configureEach {
33+
compilerOptions {
34+
jvmTarget.set(JvmTarget.JVM_1_8)
35+
}
36+
}
37+
38+
tasks.withType<Test> {
39+
useJUnitPlatform()
40+
}
41+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
plugins {
2+
kotlin("jvm")
3+
id("app.cash.burst")
4+
}
5+
6+
dependencies {
7+
testImplementation(kotlin("test"))
8+
testImplementation(libs.junit)
9+
testRuntimeOnly(libs.junit.vintage.engine)
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package app.cash.burst.tests
2+
3+
import app.cash.burst.TestFunction
4+
import app.cash.burst.TestInterceptor
5+
6+
class BasicInterceptor : TestInterceptor {
7+
override fun intercept(testFunction: TestFunction) {
8+
println("intercepting ${testFunction.functionName}")
9+
testFunction()
10+
println("intercepted ${testFunction.functionName}")
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package app.cash.burst.tests
2+
3+
import app.cash.burst.InterceptTest
4+
5+
class KotlinTestTest {
6+
@InterceptTest
7+
val interceptor = BasicInterceptor()
8+
9+
@kotlin.test.BeforeTest
10+
fun beforeTest() {
11+
println("@kotlin.test.BeforeTest before test")
12+
}
13+
14+
@kotlin.test.AfterTest
15+
fun afterTest() {
16+
println("@kotlin.test.AfterTest after test")
17+
}
18+
19+
@kotlin.test.Test
20+
fun kotlinTestTest() {
21+
println("@kotlin.test.Test running")
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package app.cash.burst.tests
2+
3+
import app.cash.burst.InterceptTest
4+
5+
class OrgJunitJupiterApiTest {
6+
@InterceptTest
7+
val interceptor = BasicInterceptor()
8+
9+
@org.junit.jupiter.api.BeforeEach
10+
fun beforeTest() {
11+
println("@org.junit.jupiter.api.BeforeEach before test")
12+
}
13+
14+
@org.junit.jupiter.api.AfterEach
15+
fun afterTest() {
16+
println("@org.junit.jupiter.api.AfterEach after test")
17+
}
18+
19+
@org.junit.jupiter.api.Test
20+
fun orgJunitJupiterApiTest() {
21+
println("@org.junit.jupiter.api.Test running")
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package app.cash.burst.tests
2+
3+
import app.cash.burst.InterceptTest
4+
5+
class OrgJunitTest {
6+
@InterceptTest
7+
val interceptor = BasicInterceptor()
8+
9+
@org.junit.Before
10+
fun beforeTest() {
11+
println("@org.junit.Before before test")
12+
}
13+
14+
@org.junit.After
15+
fun afterTest() {
16+
println("@org.junit.After after test")
17+
}
18+
19+
@org.junit.Test
20+
fun orgJunitTest() {
21+
println("@org.junit.Test running")
22+
}
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
dependencyResolutionManagement {
2+
versionCatalogs {
3+
create("libs") {
4+
from(files("../../../../../gradle/libs.versions.toml"))
5+
}
6+
}
7+
}
8+
9+
include(":lib")

burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/BurstApis.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ internal class BurstApis private constructor(
101101
}
102102

103103
val beforeTestSymbols = listOfNotNull(
104+
pluginContext.referenceClass(junit5BeforeEachClassId),
104105
pluginContext.referenceClass(junitBeforeTestClassId),
105106
pluginContext.referenceClass(kotlinBeforeTestClassId),
106107
)
107108

108109
val afterTestSymbols = listOfNotNull(
110+
pluginContext.referenceClass(junit5AfterEachClassId),
109111
pluginContext.referenceClass(junitAfterTestClassId),
110112
pluginContext.referenceClass(kotlinAfterTestClassId),
111113
)
@@ -141,6 +143,8 @@ private val junitTestClassId = junitPackage.classId("Test")
141143
private val junitBeforeTestClassId = junitPackage.classId("Before")
142144
private val junitAfterTestClassId = junitPackage.classId("After")
143145
private val junit5Package = FqPackageName("org.junit.jupiter.api")
146+
private val junit5BeforeEachClassId = junit5Package.classId("BeforeEach")
147+
private val junit5AfterEachClassId = junit5Package.classId("AfterEach")
144148
private val junit5TestClassId = junit5Package.classId("Test")
145149
private val kotlinTestPackage = FqPackageName("kotlin.test")
146150
private val kotlinTestClassId = kotlinTestPackage.classId("Test")

0 commit comments

Comments
 (0)