-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic HermesMock listener for kotest
# What Add basic implementation of a kotest extension based on the Wiremock one. # Why To provide a ready to go implementation for kotest users.
- Loading branch information
Showing
6 changed files
with
132 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[versions] | ||
kotest = "5.5.4" | ||
kotlin-logging = "6.0.3" | ||
|
||
[libraries] | ||
kotest-assertions = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" } | ||
kotest-api = { module = "io.kotest:kotest-framework-api", version.ref = "kotest" } | ||
kotest-runner = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" } | ||
kotlin-logging = { module = "io.github.oshai:kotlin-logging-jvm", version.ref = "kotlin-logging" } |
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,19 @@ | ||
plugins { | ||
kotlin("jvm") version "1.9.22" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
api(project(":hermes-mock")) | ||
implementation(libs.kotest.api) | ||
implementation(libs.kotlin.logging) | ||
testImplementation(libs.kotest.runner) | ||
testImplementation(libs.kotest.assertions) | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} |
70 changes: 70 additions & 0 deletions
70
...kotest-extension/src/main/kotlin/pl/allegro/tech/hermes/mock/kotest/HermesMockListener.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,70 @@ | ||
package pl.allegro.tech.hermes.mock.kotest | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import io.kotest.core.listeners.ProjectListener | ||
import io.kotest.core.listeners.TestListener | ||
import io.kotest.core.spec.Spec | ||
import io.kotest.core.test.TestCase | ||
import io.kotest.core.test.TestResult | ||
import pl.allegro.tech.hermes.mock.HermesMock | ||
|
||
private val logger = KotlinLogging.logger { } | ||
|
||
class HermesMockListener( | ||
private val hermesMock: HermesMock, | ||
private val listenerMode: ListenerMode | ||
) : TestListener, ProjectListener { | ||
override suspend fun beforeTest(testCase: TestCase) { | ||
if (listenerMode == ListenerMode.PER_TEST) { | ||
logger.debug { "Started Mock Hermes $listenerMode" } | ||
hermesMock.start() | ||
} | ||
} | ||
|
||
override suspend fun afterTest(testCase: TestCase, result: TestResult) { | ||
if (listenerMode == ListenerMode.PER_TEST) { | ||
logger.debug { "Stopped Mock Hermes $listenerMode" } | ||
hermesMock.stop() | ||
} | ||
} | ||
|
||
override suspend fun beforeSpec(spec: Spec) { | ||
if (listenerMode == ListenerMode.PER_SPEC) { | ||
logger.debug { "Started Mock Hermes $listenerMode" } | ||
hermesMock.start() | ||
} | ||
} | ||
|
||
override suspend fun afterSpec(spec: Spec) { | ||
if (listenerMode == ListenerMode.PER_SPEC) { | ||
logger.debug { "Stopped Mock Hermes $listenerMode" } | ||
hermesMock.stop() | ||
} | ||
} | ||
|
||
override suspend fun beforeProject() { | ||
if (listenerMode == ListenerMode.PER_PROJECT) { | ||
logger.debug { "Started Mock Hermes $listenerMode" } | ||
hermesMock.start() | ||
} | ||
} | ||
|
||
override suspend fun afterProject() { | ||
if (listenerMode == ListenerMode.PER_PROJECT) { | ||
logger.debug { "Stopped Mock Hermes $listenerMode" } | ||
hermesMock.stop() | ||
} | ||
} | ||
|
||
companion object { | ||
fun perSpec(hermesMock: HermesMock) = HermesMockListener(hermesMock, ListenerMode.PER_SPEC) | ||
fun perTest(hermesMock: HermesMock) = HermesMockListener(hermesMock, ListenerMode.PER_TEST) | ||
fun perProject(hermesMock: HermesMock) = HermesMockListener(hermesMock, ListenerMode.PER_PROJECT) | ||
} | ||
} | ||
|
||
enum class ListenerMode { | ||
PER_TEST, | ||
PER_SPEC, | ||
PER_PROJECT | ||
} |
15 changes: 15 additions & 0 deletions
15
...on/src/test/kotlin/pl/allegro/tech/hermes/mock/kotest/HermesMockListenerPerProjectTest.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,15 @@ | ||
package pl.allegro.tech.hermes.mock.kotest | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
|
||
class HermesMockListenerPerProjectTest : FunSpec({ | ||
|
||
test("should have started HermesMock") { | ||
val connection = URL("http://localhost:${ProjectConfig.HERMES_MOCK_PORT}").openConnection() as HttpURLConnection | ||
|
||
connection.responseCode shouldBe 404 | ||
} | ||
}) |
14 changes: 14 additions & 0 deletions
14
...mock-kotest-extension/src/test/kotlin/pl/allegro/tech/hermes/mock/kotest/ProjectConfig.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,14 @@ | ||
package pl.allegro.tech.hermes.mock.kotest | ||
|
||
import io.kotest.core.config.AbstractProjectConfig | ||
import io.kotest.core.extensions.Extension | ||
import pl.allegro.tech.hermes.mock.HermesMock | ||
|
||
object ProjectConfig : AbstractProjectConfig() { | ||
const val HERMES_MOCK_PORT = 9001 | ||
|
||
val hermesMock = HermesMock.Builder().withPort(HERMES_MOCK_PORT).build() | ||
val hermesMockListener = HermesMockListener(hermesMock, ListenerMode.PER_PROJECT) | ||
|
||
override fun extensions(): List<Extension> = listOf(hermesMockListener) | ||
} |
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