From 3b9616aa673fa527dd0883edb6fc5ecee92b56a5 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 27 Mar 2025 03:08:26 +0100 Subject: [PATCH] refactor testApplication --- src/test/kotlin/eventDemo/Helpers.kt | 13 +++++++++++++ .../interfaceLayer/query/GameSimulationTest.kt | 6 ++---- .../business/command/GameCommandHandlerTest.kt | 6 ++---- .../event/projection/GameStateRepositoryTest.kt | 12 +++++------- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/test/kotlin/eventDemo/Helpers.kt b/src/test/kotlin/eventDemo/Helpers.kt index cb98010..691f5d1 100644 --- a/src/test/kotlin/eventDemo/Helpers.kt +++ b/src/test/kotlin/eventDemo/Helpers.kt @@ -2,9 +2,22 @@ package eventDemo import eventDemo.business.entity.Card import eventDemo.business.entity.Deck +import eventDemo.configuration.injection.appKoinModule +import eventDemo.configuration.ktor.configuration +import io.ktor.server.config.ApplicationConfig +import org.koin.core.Koin +import org.koin.core.module.KoinApplicationDslMarker +import org.koin.dsl.koinApplication fun Deck.allCardCount(): Int = stack.size + discard.size + playersHands.values.flatten().size fun Deck.allCards(): Set = stack + discard + playersHands.values.flatten() + +@KoinApplicationDslMarker +suspend fun testApplicationWithConfig(block: suspend Koin.() -> Unit) { + koinApplication { modules(appKoinModule(ApplicationConfig("application.conf").configuration())) }.koin.apply { + block() + } +} diff --git a/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameSimulationTest.kt b/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameSimulationTest.kt index 5e7f1c6..f84b017 100644 --- a/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameSimulationTest.kt +++ b/src/test/kotlin/eventDemo/adapter/interfaceLayer/query/GameSimulationTest.kt @@ -22,9 +22,8 @@ import eventDemo.business.notification.PlayerAsPlayACardNotification import eventDemo.business.notification.PlayerWasReadyNotification import eventDemo.business.notification.TheGameWasStartedNotification import eventDemo.business.notification.WelcomeToTheGameNotification -import eventDemo.configuration.injection.Configuration -import eventDemo.configuration.injection.appKoinModule import eventDemo.libs.event.projection.ProjectionSnapshotRepositoryInMemory +import eventDemo.testApplicationWithConfig import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldHaveSize import io.kotest.matchers.equals.shouldBeEqual @@ -37,7 +36,6 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch import kotlinx.coroutines.withTimeout -import org.koin.dsl.koinApplication import kotlin.test.assertIs import kotlin.test.assertNotNull import kotlin.test.assertTrue @@ -171,7 +169,7 @@ class GameSimulationTest : } } - koinApplication { modules(appKoinModule(Configuration("redis://localhost:6379"))) }.koin.apply { + testApplicationWithConfig { val commandHandler by inject() val eventStore by inject() val playerNotificationListener by inject() diff --git a/src/test/kotlin/eventDemo/business/command/GameCommandHandlerTest.kt b/src/test/kotlin/eventDemo/business/command/GameCommandHandlerTest.kt index ddd1aa1..8f42033 100644 --- a/src/test/kotlin/eventDemo/business/command/GameCommandHandlerTest.kt +++ b/src/test/kotlin/eventDemo/business/command/GameCommandHandlerTest.kt @@ -9,8 +9,7 @@ import eventDemo.business.event.projection.projectionListener.ReactionListener import eventDemo.business.notification.CommandSuccessNotification import eventDemo.business.notification.Notification import eventDemo.business.notification.WelcomeToTheGameNotification -import eventDemo.configuration.injection.Configuration -import eventDemo.configuration.injection.appKoinModule +import eventDemo.testApplicationWithConfig import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldContain import io.kotest.matchers.equals.shouldBeEqual @@ -20,7 +19,6 @@ import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.trySendBlocking import kotlinx.coroutines.launch import kotlinx.coroutines.withTimeout -import org.koin.dsl.koinApplication import kotlin.test.assertIs import kotlin.time.Duration.Companion.seconds @@ -29,7 +27,7 @@ class GameCommandHandlerTest : FunSpec({ test("handle a command should execute the command") { withTimeout(1.seconds) { - koinApplication { modules(appKoinModule(Configuration("redis://localhost:6379"))) }.koin.apply { + testApplicationWithConfig { val commandHandler by inject() val notificationListener by inject() val gameId = GameId() diff --git a/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt b/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt index 2fe6001..00d306e 100644 --- a/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt +++ b/src/test/kotlin/eventDemo/business/event/projection/GameStateRepositoryTest.kt @@ -6,8 +6,7 @@ import eventDemo.business.event.GameEventHandler import eventDemo.business.event.event.NewPlayerEvent import eventDemo.business.event.projection.gameState.GameState import eventDemo.business.event.projection.gameState.GameStateRepository -import eventDemo.configuration.injection.Configuration -import eventDemo.configuration.injection.appKoinModule +import eventDemo.testApplicationWithConfig import io.kotest.assertions.nondeterministic.eventually import io.kotest.assertions.nondeterministic.eventuallyConfig import io.kotest.core.spec.style.FunSpec @@ -20,7 +19,6 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch import org.koin.core.context.stopKoin -import org.koin.dsl.koinApplication import kotlin.test.assertNotNull import kotlin.time.Duration.Companion.seconds @@ -32,7 +30,7 @@ class GameStateRepositoryTest : test("GameStateRepository should build the projection when a new event occurs") { val aggregateId = GameId() - koinApplication { modules(appKoinModule(Configuration("redis://localhost:6379"))) }.koin.apply { + testApplicationWithConfig { val repo = get() val eventHandler = get() eventHandler @@ -54,7 +52,7 @@ class GameStateRepositoryTest : test("get should build the last version of the state") { val aggregateId = GameId() - koinApplication { modules(appKoinModule(Configuration("redis://localhost:6379"))) }.koin.apply { + testApplicationWithConfig { val repo = get() val eventHandler = get() val projectionBus = get() @@ -90,7 +88,7 @@ class GameStateRepositoryTest : test("getUntil should build the state until the event") { repeat(10) { val aggregateId = GameId() - koinApplication { modules(appKoinModule(Configuration("redis://localhost:6379"))) }.koin.apply { + testApplicationWithConfig { val repo = get() val eventHandler = get() @@ -119,7 +117,7 @@ class GameStateRepositoryTest : test("getUntil should be concurrently secure") { val aggregateId = GameId() - koinApplication { modules(appKoinModule(Configuration("redis://localhost:6379"))) }.koin.apply { + testApplicationWithConfig { val repo = get() val eventHandler = get()