Skip to content
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

Support multiple Kotest spec instances #860

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ max_line_length = 100
# Import order can be configured with ij_java_imports_layout=...
# See documentation https://youtrack.jetbrains.com/issue/IDEA-170643#focus=streamItem-27-3708697.0-0

[*.kt]
indent_size = 4

[*.xml]
indent_size = 4
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class MicronautKotest5Context(
override fun alignMocks(context: Spec?, instance: Any) {
}

fun isApplicationContextOpen(): Boolean {
return applicationContext != null
}

fun beforeSpecClass(spec: Spec) {
if (!createBean) {
beforeClass(spec, testClass, micronautTestValue)
Expand Down Expand Up @@ -115,5 +119,4 @@ class MicronautKotest5Context(
false
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import io.micronaut.test.extensions.kotest5.annotation.MicronautTest
import kotlin.reflect.KClass
import kotlin.reflect.full.primaryConstructor

object MicronautKotest5Extension: TestListener, ConstructorExtension, TestCaseExtension {
object MicronautKotest5Extension : TestListener, ConstructorExtension, TestCaseExtension {

override suspend fun intercept(
testCase: TestCase,
execute: suspend (TestCase) -> TestResult
): TestResult {
val context = contexts[testCase.spec.javaClass.name]
return if(context != null && context.getSpecDefinition() == null) {
// val context = contexts[testCase.spec]
sksamuel marked this conversation as resolved.
Show resolved Hide resolved
val context = testCase.spec.context()
return if (context != null && context.getSpecDefinition() == null) {
// It's a MicronautTest test where the bean doesn't exist
TestResult.Ignored
} else {
Expand All @@ -43,55 +44,61 @@ object MicronautKotest5Extension: TestListener, ConstructorExtension, TestCaseEx
}
}

val contexts: MutableMap<String, MicronautKotest5Context> = mutableMapOf()
// val contexts: MutableMap<Spec, MicronautKotest5Context> = mutableMapOf()

val contexts: MutableMap<String, List<MicronautKotest5Context>> = mutableMapOf()

private fun Spec.context() =
contexts[javaClass.name]?.find { it.bean == this } ?: contexts[javaClass.name]?.find { it.bean == null }

override suspend fun beforeSpec(spec: Spec) {
contexts[spec.javaClass.name]?.beforeSpecClass(spec)
spec.context()?.beforeSpecClass(spec)
}

override suspend fun afterSpec(spec: Spec) {
contexts[spec.javaClass.name]?.afterSpecClass(spec)
spec.context()?.afterSpecClass(spec)
}

override suspend fun beforeTest(testCase: TestCase) {
contexts[testCase.spec.javaClass.name]?.beforeTest(testCase)
testCase.spec.context()?.beforeTest(testCase)
}

override suspend fun afterTest(testCase: TestCase, result: TestResult) {
contexts[testCase.spec.javaClass.name]?.afterTest(testCase, result)
testCase.spec.context()?.afterTest(testCase, result)
}

override suspend fun beforeInvocation(testCase: TestCase, iteration: Int) {
contexts[testCase.spec.javaClass.name]?.beforeInvocation(testCase)
testCase.spec.context()?.beforeInvocation(testCase)
}

override suspend fun afterInvocation(testCase: TestCase, iteration: Int) {
contexts[testCase.spec.javaClass.name]?.afterInvocation(testCase)
testCase.spec.context()?.afterInvocation(testCase)
}

override suspend fun beforeContainer(testCase: TestCase) {
contexts[testCase.spec.javaClass.name]?.beforeInvocation(testCase)
testCase.spec.context()?.beforeInvocation(testCase)
}

override suspend fun afterContainer(testCase: TestCase, result: TestResult) {
contexts[testCase.spec.javaClass.name]?.afterInvocation(testCase)
testCase.spec.context()?.afterInvocation(testCase)
}

@Suppress("UNCHECKED_CAST")
override fun <T : Spec> instantiate(clazz: KClass<T>): Spec? {
val constructor = clazz.primaryConstructor
val testClass: Class<Any> = clazz.java as Class<Any>
val micronautTestValue = testClass
.annotations
.filterIsInstance<MicronautTest>()
.map { micronautTest -> buildValueObject(micronautTest) }
.firstOrNull()
.annotations
.filterIsInstance<MicronautTest>()
.map { micronautTest -> buildValueObject(micronautTest) }
.firstOrNull()
return if (micronautTestValue == null) {
null
} else {
val createBean = constructor != null && constructor.parameters.isNotEmpty()
val context = MicronautKotest5Context(testClass, micronautTestValue, createBean)
contexts[testClass.name] = context
val specContexts = contexts[testClass.name] ?: emptyList()
contexts[testClass.name] = specContexts + context
if (createBean) {
context.bean
} else {
Expand All @@ -102,17 +109,17 @@ object MicronautKotest5Extension: TestListener, ConstructorExtension, TestCaseEx

private fun buildValueObject(micronautTest: MicronautTest): MicronautTestValue {
return MicronautTestValue(
micronautTest.application.java,
micronautTest.environments,
micronautTest.packages,
micronautTest.propertySources,
micronautTest.rollback,
micronautTest.transactional,
micronautTest.rebuildContext,
micronautTest.contextBuilder.map { kClass -> kClass.java }.toTypedArray(),
micronautTest.transactionMode,
micronautTest.startApplication,
false
micronautTest.application.java,
micronautTest.environments,
micronautTest.packages,
micronautTest.propertySources,
micronautTest.rollback,
micronautTest.transactional,
micronautTest.rebuildContext,
micronautTest.contextBuilder.map { kClass -> kClass.java }.toTypedArray(),
micronautTest.transactionMode,
micronautTest.startApplication,
false
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.micronaut.test.kotest5

import io.kotest.core.spec.style.FunSpec
import io.kotest.inspectors.forAll
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldNotBe
import io.micronaut.test.extensions.kotest5.MicronautKotest5Extension
import io.micronaut.test.extensions.kotest5.annotation.MicronautTest

class IsolationModeTest : FunSpec({

test("extension should support multiple instances per spec to handle isolation modes") {
val a = MicronautKotest5Extension.instantiate(IsolatedTests::class).shouldNotBeNull()
val b = MicronautKotest5Extension.instantiate(IsolatedTests::class).shouldNotBeNull()
a.hashCode() shouldNotBe b.hashCode()
MicronautKotest5Extension.contexts[IsolatedTests::class.java.name]
.shouldNotBeNull()
.shouldHaveSize(2)
MicronautKotest5Extension.contexts[IsolatedTests::class.java.name]
.shouldNotBeNull()
.forAll { it.isApplicationContextOpen() }
MicronautKotest5Extension.afterSpec(a)
MicronautKotest5Extension.afterSpec(b)
MicronautKotest5Extension.contexts[IsolatedTests::class.java.name]
.shouldNotBeNull()
.forAll { !it.isApplicationContextOpen() }
}
})

@MicronautTest
// need a parameter here so the extension instantiates the spec
private class IsolatedTests(private val mathService: MathService) : FunSpec() {
init {
test("test1") {}
test("test2") {}
}
}