Skip to content

Commit e1ab99b

Browse files
authored
Move wiremock to top level (#4737)
<!-- If this PR updates documentation, please update all relevant versions of the docs, see: https://github.com/kotest/kotest/tree/master/documentation/versioned_docs The documentation at https://github.com/kotest/kotest/tree/master/documentation/docs is the documentation for the next minor or major version _TO BE RELEASED_ -->
1 parent fb04061 commit e1ab99b

File tree

8 files changed

+229
-3
lines changed

8 files changed

+229
-3
lines changed

gradle/libs.versions.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ spring = "5.3.39"
2525
byte-buddy = "1.15.11"
2626
allure = "2.24.0"
2727
koin = "3.5.6"
28+
wiremock = "3.11.0"
2829
mockserver = "5.15.0"
2930
fuel = "2.3.1"
3031
konform = "0.10.0"
@@ -58,6 +59,8 @@ arrow-core = { module = "io.arrow-kt:arrow-core", version.ref = "arrow" }
5859
arrow-optics = { module = "io.arrow-kt:arrow-optics", version.ref = "arrow" }
5960
arrow-functions = { module = "io.arrow-kt:arrow-functions", version.ref = "arrow" }
6061

62+
fuel = { module = "com.github.kittinunf.fuel:fuel", version.ref = "fuel" }
63+
6164
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
6265
koin-test = { module = "io.insert-koin:koin-test", version.ref = "koin" }
6366

@@ -69,8 +72,6 @@ junit-platform-api = { module = "org.junit.platform:junit-platform-suite-api", v
6972
junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit-jupiter" }
7073
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit-jupiter" }
7174

72-
fuel = { module = "com.github.kittinunf.fuel:fuel", version.ref = "fuel" }
73-
7475
mockserver-client-java = { module = "org.mock-server:mockserver-client-java", version.ref = "mockserver" }
7576
mockserver-netty = { module = "org.mock-server:mockserver-netty", version.ref = "mockserver" }
7677

@@ -104,6 +105,8 @@ byte-buddy = { group = "net.bytebuddy", name = "byte-buddy", version.ref = "byte
104105
allure-commons = { module = "io.qameta.allure:allure-java-commons", version.ref = "allure" }
105106
jackson-module-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version = "2.15.2" }
106107

108+
wiremock = { module = "org.wiremock:wiremock-standalone", version.ref = "wiremock" }
109+
107110
# Gradle plugins
108111
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
109112
testlogger-gradle-plugin = { module = "com.adarshr:gradle-test-logger-plugin", version.ref = "gradle-test-logger-plugin" }

kotest-extensions/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ kotlin {
2323
}
2424
}
2525
}
26-
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public final class io/kotest/extensions/wiremock/ListenerMode : java/lang/Enum {
2+
public static final field PER_SPEC Lio/kotest/extensions/wiremock/ListenerMode;
3+
public static final field PER_TEST Lio/kotest/extensions/wiremock/ListenerMode;
4+
public static fun getEntries ()Lkotlin/enums/EnumEntries;
5+
public static fun valueOf (Ljava/lang/String;)Lio/kotest/extensions/wiremock/ListenerMode;
6+
public static fun values ()[Lio/kotest/extensions/wiremock/ListenerMode;
7+
}
8+
9+
public final class io/kotest/extensions/wiremock/WireMockListener : io/kotest/core/listeners/TestListener {
10+
public static final field Companion Lio/kotest/extensions/wiremock/WireMockListener$Companion;
11+
public fun <init> (Lcom/github/tomakehurst/wiremock/WireMockServer;Lio/kotest/extensions/wiremock/ListenerMode;)V
12+
public fun afterAny (Lio/kotest/core/test/TestCase;Lio/kotest/core/test/TestResult;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
13+
public fun afterContainer (Lio/kotest/core/test/TestCase;Lio/kotest/core/test/TestResult;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
14+
public fun afterEach (Lio/kotest/core/test/TestCase;Lio/kotest/core/test/TestResult;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
15+
public fun afterInvocation (Lio/kotest/core/test/TestCase;ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
16+
public fun afterSpec (Lio/kotest/core/spec/Spec;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
17+
public fun afterTest (Lio/kotest/core/test/TestCase;Lio/kotest/core/test/TestResult;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
18+
public fun beforeAny (Lio/kotest/core/test/TestCase;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
19+
public fun beforeContainer (Lio/kotest/core/test/TestCase;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
20+
public fun beforeEach (Lio/kotest/core/test/TestCase;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
21+
public fun beforeInvocation (Lio/kotest/core/test/TestCase;ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
22+
public fun beforeSpec (Lio/kotest/core/spec/Spec;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
23+
public fun beforeTest (Lio/kotest/core/test/TestCase;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
24+
public fun finalizeSpec (Lkotlin/reflect/KClass;Ljava/util/Map;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
25+
public fun prepareSpec (Lkotlin/reflect/KClass;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
26+
}
27+
28+
public final class io/kotest/extensions/wiremock/WireMockListener$Companion {
29+
public final fun perSpec (Lcom/github/tomakehurst/wiremock/WireMockServer;)Lio/kotest/extensions/wiremock/WireMockListener;
30+
public final fun perTest (Lcom/github/tomakehurst/wiremock/WireMockServer;)Lio/kotest/extensions/wiremock/WireMockListener;
31+
}
32+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
plugins {
2+
id("kotest-jvm-conventions")
3+
id("kotest-publishing-conventions")
4+
}
5+
6+
kotlin {
7+
sourceSets {
8+
val jvmMain by getting {
9+
dependencies {
10+
implementation(projects.kotestFramework.kotestFrameworkEngine)
11+
api(libs.wiremock)
12+
}
13+
}
14+
val jvmTest by getting {
15+
dependencies {
16+
implementation(libs.fuel)
17+
}
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.kotest.extensions.wiremock
2+
3+
import com.github.tomakehurst.wiremock.WireMockServer
4+
import io.kotest.core.listeners.TestListener
5+
import io.kotest.core.spec.Spec
6+
import io.kotest.core.test.TestCase
7+
import io.kotest.core.test.TestResult
8+
9+
/**
10+
* WiremockListener starts the given wiremock server before every spec/test and stop that
11+
* after every spec/test based on [listenerMode].
12+
*
13+
* @see [ListenerMode]
14+
* */
15+
class WireMockListener(
16+
private val server: WireMockServer,
17+
private val listenerMode: ListenerMode
18+
) : TestListener {
19+
20+
override suspend fun beforeTest(testCase: TestCase) {
21+
if (listenerMode == ListenerMode.PER_TEST) {
22+
server.start()
23+
}
24+
}
25+
26+
override suspend fun afterTest(testCase: TestCase, result: TestResult) {
27+
if (listenerMode == ListenerMode.PER_TEST) {
28+
server.stop()
29+
}
30+
}
31+
32+
override suspend fun beforeSpec(spec: Spec) {
33+
if (listenerMode == ListenerMode.PER_SPEC) {
34+
server.start()
35+
}
36+
}
37+
38+
override suspend fun afterSpec(spec: Spec) {
39+
if (listenerMode == ListenerMode.PER_SPEC) {
40+
server.stop()
41+
}
42+
}
43+
44+
companion object {
45+
fun perSpec(wireMockServer: WireMockServer) = WireMockListener(wireMockServer, ListenerMode.PER_SPEC)
46+
47+
fun perTest(wireMockServer: WireMockServer) = WireMockListener(wireMockServer, ListenerMode.PER_TEST)
48+
}
49+
}
50+
51+
enum class ListenerMode {
52+
PER_TEST,
53+
PER_SPEC
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package io.kotest.extensions.wiremock
2+
3+
import com.github.tomakehurst.wiremock.WireMockServer
4+
import com.github.tomakehurst.wiremock.client.WireMock
5+
import io.kotest.assertions.throwables.shouldNotThrowAny
6+
import io.kotest.assertions.throwables.shouldThrow
7+
import io.kotest.core.listeners.TestListener
8+
import io.kotest.core.spec.Spec
9+
import io.kotest.core.spec.style.FunSpec
10+
import io.kotest.core.test.TestCase
11+
import io.kotest.core.test.TestResult
12+
import io.kotest.matchers.shouldBe
13+
import java.net.ConnectException
14+
import java.net.HttpURLConnection
15+
import java.net.URL
16+
17+
@Suppress("BlockingMethodInNonBlockingContext")
18+
class WiremockListenerPerSpecTest : FunSpec({
19+
val wireMockServer = WireMockServer(9000)
20+
21+
extension(WireMockListener.perSpec(wireMockServer))
22+
23+
extension(object : TestListener {
24+
override suspend fun afterTest(testCase: TestCase, result: TestResult) {
25+
wireMockServer.stubFor(
26+
WireMock.get(WireMock.urlEqualTo("/after-test"))
27+
.willReturn(WireMock.ok())
28+
)
29+
shouldNotThrowAny {
30+
val connection = URL("http://localhost:9000/after-test").openConnection() as HttpURLConnection
31+
connection.responseCode shouldBe 200
32+
}
33+
}
34+
35+
override suspend fun afterSpec(spec: Spec) {
36+
wireMockServer.stubFor(
37+
WireMock.get(WireMock.urlEqualTo("/after-spec"))
38+
.willReturn(WireMock.ok())
39+
)
40+
41+
shouldThrow<ConnectException> {
42+
val connection = URL("http://localhost:9000/after-spec").openConnection() as HttpURLConnection
43+
connection.responseCode shouldBe 200
44+
}
45+
}
46+
})
47+
48+
test("should have started wiremock server") {
49+
wireMockServer.stubFor(
50+
WireMock.get(WireMock.urlEqualTo("/test"))
51+
.willReturn(WireMock.ok())
52+
)
53+
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection
54+
connection.responseCode shouldBe 200
55+
}
56+
57+
test("should have started wiremock server for second test as well") {
58+
wireMockServer.stubFor(
59+
WireMock.get(WireMock.urlEqualTo("/second-test"))
60+
.willReturn(WireMock.ok())
61+
)
62+
val connection = URL("http://localhost:9000/second-test").openConnection() as HttpURLConnection
63+
connection.responseCode shouldBe 200
64+
}
65+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.kotest.extensions.wiremock
2+
3+
import com.github.tomakehurst.wiremock.WireMockServer
4+
import com.github.tomakehurst.wiremock.client.WireMock.get
5+
import com.github.tomakehurst.wiremock.client.WireMock.ok
6+
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
7+
import io.kotest.assertions.throwables.shouldThrow
8+
import io.kotest.core.listeners.TestListener
9+
import io.kotest.core.spec.style.FunSpec
10+
import io.kotest.core.test.TestCase
11+
import io.kotest.core.test.TestResult
12+
import io.kotest.matchers.shouldBe
13+
import java.net.ConnectException
14+
import java.net.HttpURLConnection
15+
import java.net.URL
16+
17+
@Suppress("BlockingMethodInNonBlockingContext")
18+
class WiremockListenerPerTestTest : FunSpec({
19+
val wireMockServer = WireMockServer(9000)
20+
21+
extension(WireMockListener.perTest(wireMockServer))
22+
extension(object : TestListener {
23+
override suspend fun afterTest(testCase: TestCase, result: TestResult) {
24+
shouldThrow<ConnectException> {
25+
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection
26+
connection.responseCode
27+
}
28+
}
29+
})
30+
31+
32+
test("should have started wiremock server") {
33+
wireMockServer.stubFor(
34+
get(urlEqualTo("/test"))
35+
.willReturn(ok())
36+
)
37+
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection
38+
connection.responseCode shouldBe 200
39+
}
40+
41+
test("should have started wiremock server for second test as well") {
42+
wireMockServer.stubFor(
43+
get(urlEqualTo("/second-test"))
44+
.willReturn(ok())
45+
)
46+
val connection = URL("http://localhost:9000/second-test").openConnection() as HttpURLConnection
47+
connection.responseCode shouldBe 200
48+
}
49+
})

settings.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ include(
131131
// extensions that adapt junit extensions into kotest extensions
132132
":kotest-extensions:kotest-extensions-junit5",
133133

134+
// adds support for the wiremock framework - see more https://www.wiremock.io/
135+
":kotest-extensions:kotest-extensions-wiremock",
136+
134137
// the tests modules each test a piece of functionality
135138
// it is useful to have separate modules so each can set their own project config that
136139
// may be required as part of the tests

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy