-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runner): Add a custom robolectric runner which is able to manage…
… JUnit Jupiter annotations (#5) * feat(runner): Add a custom robolectric runner which is able to manage JUnit Jupiter annotations * chore: Update git ignore * chore: Update parameterizedTestAnnotation to lazy * fix: logging to trace
- Loading branch information
Showing
15 changed files
with
412 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
allprojects { | ||
group = 'tech.apter.junit5.jupiter' | ||
version = '1.0-SNAPSHOT' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
android.useAndroidX=true | ||
kotlin.code.style=official |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,39 @@ | ||
plugins { | ||
alias(libs.plugins.kotlinJvm) | ||
alias(libs.plugins.androidLibrary) | ||
alias(libs.plugins.kotlinAndroid) | ||
alias(libs.plugins.androidJUnit5) | ||
} | ||
|
||
group = 'tech.apter.junit5.jupiter' | ||
version = '1.0-SNAPSHOT' | ||
android { | ||
defaultConfig { | ||
namespace = "$group.$name" | ||
compileOptions { | ||
compileSdk libs.versions.androidCompileSdk.get().toInteger() | ||
} | ||
} | ||
testOptions { | ||
unitTests.all { | ||
useJUnitPlatform() | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation libs.robolectric | ||
implementation libs.junit4 | ||
implementation platform(libs.junit5Bom) | ||
implementation libs.bundles.junit5 | ||
testImplementation libs.junit4 | ||
testImplementation libs.androidxTestExtJunit | ||
testImplementation libs.kotlinTestJUnit5 | ||
testImplementation libs.junit5JupiterParams | ||
testRuntimeOnly libs.junit5JupiterEngine | ||
implementation(libs.guava) { | ||
because "CVE-2023-2976 7.1 " + | ||
"Transitive Files or Directories Accessible to External Parties vulnerability with High severity found" | ||
} | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} | ||
|
||
kotlin { | ||
jvmToolchain(libs.versions.jvmToolchain.get().toInteger()) | ||
} |
110 changes: 110 additions & 0 deletions
110
.../kotlin/tech/apter/junit/jupiter/robolectric/internal/BlockJUnit4ClassRunnerExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package tech.apter.junit.jupiter.robolectric.internal | ||
|
||
import org.junit.After | ||
import org.junit.AfterClass | ||
import org.junit.Before | ||
import org.junit.BeforeClass | ||
import org.junit.jupiter.api.AfterAll | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Disabled | ||
import org.junit.jupiter.api.Test | ||
import org.junit.runners.BlockJUnit4ClassRunner | ||
import org.junit.runners.model.FrameworkMethod | ||
import java.util.Collections | ||
|
||
private val parameterizedTestAnnotation: Class<out Annotation>? by lazy { | ||
try { | ||
@Suppress("UNCHECKED_CAST") | ||
Class.forName("org.junit.jupiter.params.ParameterizedTest") as? Class<out Annotation> | ||
} catch (@Suppress("SwallowedException") e: ClassNotFoundException) { | ||
null | ||
} | ||
} | ||
|
||
internal fun BlockJUnit4ClassRunner.computeJUnit5TestMethods(): MutableList<FrameworkMethod> { | ||
val testMethods = testClass.getAnnotatedMethods(Test::class.java) | ||
val parameterizedTestMethods = if (parameterizedTestAnnotation == null) { | ||
emptyList() | ||
} else { | ||
testClass.getAnnotatedMethods(parameterizedTestAnnotation) | ||
} | ||
|
||
val methods = mutableListOf<FrameworkMethod>() | ||
methods.addAll(testMethods) | ||
methods.addAll(parameterizedTestMethods) | ||
return Collections.unmodifiableList(methods) | ||
} | ||
|
||
@Suppress("UnusedReceiverParameter") | ||
internal fun BlockJUnit4ClassRunner.isJUnit5Ignored(child: FrameworkMethod) = | ||
child.getAnnotation(Disabled::class.java) != null | ||
|
||
/** | ||
* Adds to errors if any method in this class is annotated with annotation, but: | ||
* | ||
* * is not public, or | ||
* * takes parameters, or | ||
* * returns something other than void, or | ||
* * is static (given isStatic is false), or | ||
* * is not static (given isStatic is true). | ||
*/ | ||
internal fun BlockJUnit4ClassRunner.validatePublicVoidNoArgJUnit5Methods( | ||
annotation: Class<out Annotation>, | ||
isStatic: Boolean, errors: MutableList<Throwable>, | ||
) { | ||
if (annotation == org.junit.Test::class.java) { | ||
validatePublicVoidNoArgMethods(Test::class.java, isStatic, errors) | ||
parameterizedTestAnnotation?.let { validatePublicVoidArgMethods(it, isStatic, errors) } | ||
} else { | ||
val jUnit5Annotation = when (annotation) { | ||
Before::class.java -> BeforeEach::class.java | ||
After::class.java -> After::class.java | ||
BeforeClass::class.java -> BeforeAll::class.java | ||
AfterClass::class.java -> AfterAll::class.java | ||
else -> annotation | ||
} | ||
validatePublicVoidNoArgMethods(jUnit5Annotation, isStatic, errors) | ||
} | ||
} | ||
|
||
/** | ||
* Adds to `errors` if any method in this class is annotated with | ||
* `annotation`, but: | ||
* | ||
* * is not public, or | ||
* * takes parameters, or | ||
* * returns something other than void, or | ||
* * is static (given `isStatic is false`), or | ||
* * is not static (given `isStatic is true`). | ||
*/ | ||
internal fun BlockJUnit4ClassRunner.validatePublicVoidNoArgMethods( | ||
annotation: Class<out Annotation>, | ||
isStatic: Boolean, | ||
errors: List<Throwable>, | ||
) { | ||
val methods: List<FrameworkMethod> = testClass.getAnnotatedMethods(annotation) | ||
for (eachTestMethod in methods) { | ||
eachTestMethod.validatePublicVoidNoArg(isStatic, errors) | ||
} | ||
} | ||
|
||
/** | ||
* Adds to `errors` if any method in this class is annotated with | ||
* `annotation`, but: | ||
* | ||
* * is not public, or | ||
* * returns something other than void, or | ||
* * is static (given `isStatic is false`), or | ||
* * is not static (given `isStatic is true`). | ||
*/ | ||
internal fun BlockJUnit4ClassRunner.validatePublicVoidArgMethods( | ||
@Suppress("SameParameterValue") annotation: Class<out Annotation>, | ||
isStatic: Boolean, | ||
errors: List<Throwable>, | ||
) { | ||
val methods = testClass.getAnnotatedMethods(annotation) | ||
for (eachTestMethod in methods) { | ||
eachTestMethod.validatePublicVoid(isStatic, errors) | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
.../main/kotlin/tech/apter/junit/jupiter/robolectric/internal/JUnit5RobolectricTestRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package tech.apter.junit.jupiter.robolectric.internal | ||
|
||
import org.junit.runners.model.FrameworkMethod | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.internal.SandboxManager.SandboxBuilder | ||
import org.robolectric.internal.SandboxTestRunner | ||
import org.robolectric.internal.bytecode.InstrumentationConfiguration | ||
import org.robolectric.internal.bytecode.Sandbox | ||
import java.lang.reflect.Method | ||
|
||
internal class JUnit5RobolectricTestRunner(clazz: Class<*>) : | ||
RobolectricTestRunner(clazz, injector) { | ||
private val logger get() = createLogger() | ||
fun frameworkMethod(method: Method): FrameworkMethod = children.first { it.name == method.name } | ||
|
||
fun bootstrapSdkEnvironment(): Sandbox = sdkEnvironment(children.first()) | ||
|
||
fun sdkEnvironment(frameworkMethod: FrameworkMethod): Sandbox { | ||
return getSandbox(frameworkMethod).also { | ||
configureSandbox(it, frameworkMethod) | ||
} | ||
} | ||
|
||
fun runBeforeTest( | ||
sdkEnvironment: Sandbox, | ||
frameworkMethod: FrameworkMethod, | ||
bootstrappedMethod: Method, | ||
) { | ||
logger.trace { | ||
"runBeforeTest ${bootstrappedMethod.declaringClass.simpleName}::${bootstrappedMethod.name}" | ||
} | ||
super.beforeTest(sdkEnvironment, frameworkMethod, bootstrappedMethod) | ||
} | ||
|
||
fun runAfterTest(frameworkMethod: FrameworkMethod, bootstrappedMethod: Method) { | ||
logger.trace { | ||
"runAfterTest${bootstrappedMethod.declaringClass.simpleName}::${bootstrappedMethod.name}" | ||
} | ||
try { | ||
super.afterTest(frameworkMethod, bootstrappedMethod) | ||
} finally { | ||
super.finallyAfterTest(frameworkMethod) | ||
} | ||
} | ||
|
||
override fun createClassLoaderConfig(method: FrameworkMethod): InstrumentationConfiguration { | ||
return InstrumentationConfiguration.Builder(super.createClassLoaderConfig(method)) | ||
.doNotAcquirePackage("tech.apter.junit.jupiter.robolectric").build() | ||
} | ||
|
||
override fun computeTestMethods() = computeJUnit5TestMethods() | ||
|
||
override fun isIgnored(child: FrameworkMethod) = isJUnit5Ignored(child) | ||
|
||
override fun validatePublicVoidNoArgMethods( | ||
annotation: Class<out Annotation>, | ||
isStatic: Boolean, | ||
errors: MutableList<Throwable>, | ||
) = validatePublicVoidNoArgJUnit5Methods(annotation, isStatic, errors) | ||
|
||
override fun getHelperTestRunner(bootstrappedTestClass: Class<*>): SandboxTestRunner.HelperTestRunner = | ||
HelperTestRunner(bootstrappedTestClass) | ||
|
||
private class HelperTestRunner(bootstrappedTestClass: Class<*>) : | ||
RobolectricTestRunner.HelperTestRunner(bootstrappedTestClass) { | ||
override fun computeTestMethods(): MutableList<FrameworkMethod> = computeJUnit5TestMethods() | ||
|
||
override fun isIgnored(child: FrameworkMethod) = isJUnit5Ignored(child) | ||
|
||
override fun validatePublicVoidNoArgMethods( | ||
annotation: Class<out Annotation>, | ||
isStatic: Boolean, | ||
errors: MutableList<Throwable>, | ||
) = validatePublicVoidNoArgJUnit5Methods(annotation, isStatic, errors) | ||
} | ||
|
||
private companion object { | ||
private val injector = defaultInjector() | ||
.bind(SandboxBuilder::class.java, JUnit5RobolectricSandboxBuilder::class.java) | ||
.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.