-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Spike support for Kotlin suspending functions #4515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
if (parameterTypes[parameterTypes.length - 1].getName().equals("kotlin.coroutines.Continuation")) { | ||
// Kotlin suspend functions have a Continuation parameter at the end that should not be included | ||
parameterTypes = Arrays.copyOf(parameterTypes, parameterTypes.length - 1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 This removes the Continuation
parameter from display names because it's generated by the Kotlin compiler.
@@ -1,5 +1,5 @@ | |||
plugins { | |||
id("junitbuild.kotlin-library-conventions") | |||
id("junitbuild.java-library-conventions") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 We were not actually using Kotlin in this project.
Yes, Spring Framework provides transparent Kotlin support without Kotlin-specific Spring artifacts.
Probably not. |
@sbrannen Thanks for the pointers! I think we have two options:
As far as manual effort is concerned, (1) requires adding at least two dependencies ( Are there any other pros/cons? |
24b1f79
to
83aaa48
Compare
83aaa48
to
329f2d6
Compare
@@ -155,9 +153,6 @@ private static <A extends Annotation> List<ArgumentSetLifecycleMethod> findLifec | |||
List<Method> methods = findAnnotatedMethods(testClass, annotationType, traversalMode); | |||
|
|||
return methods.stream() // | |||
.filter(ModifierSupport::isNotPrivate) // | |||
.filter(testInstanceLifecycle == PER_METHOD ? ModifierSupport::isStatic : __ -> true) // | |||
.filter(ReflectionUtils::returnsPrimitiveVoid) // |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 This is already validated in junit-jupiter-engine
so there's no need to do so again.
IMO having an explicit artifact for Kotlin support is sending a clear message: JUnit Jupiter wants to support Kotlin. That's a win in my book. And it opens the door for further support, e.g. providing Kotlin-style API for some constructs. |
override fun getReturnType() = | ||
with(kotlinFunction.returnType.jvmErasure) { | ||
if (this == Unit::class) { | ||
Void.TYPE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Void.TYPE | |
void.class |
|
||
public MethodAdapterRegistry() { | ||
// Load and instantiate factories eagerly to avoid GraalVM issue | ||
this.factories = ServiceLoader.load(MethodAdapterFactory.class).stream() // |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ Use getDefaultClassLoader()
here
Superseded by #4530. |
Overview
Issue: #1914
Given the function definition of
suspend fun suspendingTest(message: String): Unit
, the Kotlin compiler generates byte code that results in Java's reflection API seeing:The return type is
java.lang.Object
and an extra parameter of typekotlin.coroutines.Continuation<? super T>
whereT
is the declared return type is added. SinceContinuation<...>
can be used as a method parameter for a regular function, its presence is insufficient to determine whether a method is a suspending function. Thus, a dependency onkotlin-reflect
is needed soKFunction.isSuspend
can be called.If a suspending function is detected, it can be called using
KCallables.callSuspend
(also fromkotlin-reflect
) while wrapping it inrunBlocking
fromkotlinx-coroutines
.To avoid the extra dependencies from being added to
junit-jupiter-engine
, the prototype in this PR adds a newjunit-jupiter-kotlin
artifact. The former defines a newMethodAdapterFactory
interface that the latter implements viaServiceLoader
. The returnedMethodAdapter
is used for validation (return type), parameter resolution (ignore theContinuation
parameter), and invocation (viaKCallables.callSuspend
rather thanReflectionUtils
).The PR adds support for using
suspend
on@Test
,@TestTemplate
(including but not limited to@ParameterizedTest
and@RepeatedTest
),@TestFactory
, and all lifecycle methods. Moreover, it also adds support for calling suspend methods viaExecutableInvoker
which is used to support@After
/@BeforeClassTemplateInvocationCallback
lifecycle methods. See theKotlinSuspendFunctionsTests
test class for a demo.Open Questions
junit-jupiter-kotlin
artifact be avoided somehow without forcing Kotlin dependencies on plain-Java users and without forcing Kotlin users to declare extra dependencies onkotlin-reflect
andkotlinx-coroutines
?I hereby agree to the terms of the JUnit Contributor License Agreement.
Definition of Done
@API
annotations