Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit 0b605a2

Browse files
committed
Reorganize Testing
1 parent 8aafc3b commit 0b605a2

File tree

7 files changed

+60
-100
lines changed

7 files changed

+60
-100
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ version = Ci.version
1414
repositories {
1515
mavenCentral()
1616
maven {
17-
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
17+
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots")
1818
}
1919
}
2020

@@ -23,6 +23,7 @@ dependencies {
2323
implementation(libs.kotest.api)
2424
testImplementation(libs.kotest.runner)
2525
testImplementation(libs.kotest.assertions)
26+
testImplementation(libs.fuel)
2627
}
2728

2829
tasks.test {

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
[versions]
22
wiremock = "3.6.0"
33
kotest = "5.5.4"
4+
fuel = "2.3.1"
45

56
[libraries]
67
kotest-assertions = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
78
kotest-api = { module = "io.kotest:kotest-framework-api", version.ref = "kotest" }
89
kotest-runner = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" }
910

11+
fuel = { module = "com.github.kittinunf.fuel:fuel", version.ref = "fuel" }
12+
1013
wiremock = { module = "org.wiremock:wiremock-standalone", version.ref = "wiremock" }

src/test/kotlin/io/kotest/extensions/wiremock/ProjectConfig.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import com.github.tomakehurst.wiremock.client.WireMock
55
import io.kotest.core.config.AbstractProjectConfig
66

77
object ProjectConfig : AbstractProjectConfig() {
8-
const val WIREMOCK_SERVER_PORT = 9001
8+
private const val ProjectWiremockServerPort = 9001
99

10-
val wiremock = WireMockServer(WIREMOCK_SERVER_PORT)
11-
val wiremockListener = WireMockListener(wiremock, ListenerMode.PER_PROJECT)
10+
private val projectWiremock = WireMockServer(ProjectWiremockServerPort)
11+
private val wiremockListener = WireMockListener.perProject(projectWiremock)
1212

1313
override fun extensions() = listOf(wiremockListener)
1414

1515
override suspend fun beforeProject() {
16-
WireMock.configureFor(WIREMOCK_SERVER_PORT)
16+
WireMock.configureFor(ProjectWiremockServerPort)
1717
}
1818
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.stubFor
7+
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
8+
import com.github.tomakehurst.wiremock.stubbing.StubMapping
9+
10+
fun stubOk(url: String) {
11+
stubFor(
12+
get(urlEqualTo(url))
13+
.willReturn(ok())
14+
)
15+
}
16+
17+
fun WireMockServer.stubOk(url: String): StubMapping = stubFor(
18+
get(urlEqualTo(url))
19+
)
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
package io.kotest.extensions.wiremock
22

3-
import com.github.tomakehurst.wiremock.client.WireMock.get
4-
import com.github.tomakehurst.wiremock.client.WireMock.ok
5-
import com.github.tomakehurst.wiremock.client.WireMock.stubFor
6-
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
3+
import com.github.kittinunf.fuel.httpGet
74
import io.kotest.core.spec.style.FunSpec
85
import io.kotest.matchers.shouldBe
9-
import java.net.HttpURLConnection
10-
import java.net.URL
116

12-
@Suppress("BlockingMethodInNonBlockingContext")
137
class WiremockListenerPerProjectTest : FunSpec({
148
test("should have started wiremock server") {
15-
stubFor(
16-
get(urlEqualTo("/test"))
17-
.willReturn(ok())
18-
)
19-
val connection = URL("http://localhost:9001/test").openConnection() as HttpURLConnection
20-
connection.responseCode shouldBe 200
9+
stubOk("/test")
10+
11+
val (request, response, result) = "http://localhost:9001/test".httpGet().response()
12+
response.statusCode shouldBe 200
2113
}
2214

2315
test("should have started wiremock server for second test as well") {
24-
stubFor(
25-
get(urlEqualTo("/second-test"))
26-
.willReturn(ok())
27-
)
28-
val connection = URL("http://localhost:9001/second-test").openConnection() as HttpURLConnection
29-
connection.responseCode shouldBe 200
16+
stubOk("/second-test")
17+
val (request, response, result) = "http://localhost:9001/second-test".httpGet().response()
18+
19+
response.statusCode shouldBe 200
3020
}
3121
})
22+
Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,28 @@
11
package io.kotest.extensions.wiremock
22

3+
import com.github.kittinunf.fuel.httpGet
34
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
95
import io.kotest.core.spec.style.FunSpec
10-
import io.kotest.core.test.TestCase
11-
import io.kotest.core.test.TestResult
126
import io.kotest.matchers.shouldBe
13-
import java.net.ConnectException
14-
import java.net.HttpURLConnection
15-
import java.net.URL
167

17-
@Suppress("BlockingMethodInNonBlockingContext")
188
class WiremockListenerPerSpecTest : FunSpec({
199
val wireMockServer = WireMockServer(9000)
2010

2111
listener(WireMockListener.perSpec(wireMockServer))
2212

23-
listener(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-
}
3413

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-
})
14+
test("should have started wiremock server") {
15+
wireMockServer.stubOk("/test")
4716

17+
val (request, response, result) = "http://localhost:9000/test".httpGet().response()
4818

49-
test("should have started wiremock server") {
50-
wireMockServer.stubFor(
51-
WireMock.get(WireMock.urlEqualTo("/test"))
52-
.willReturn(WireMock.ok())
53-
)
54-
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection
55-
connection.responseCode shouldBe 200
19+
response.statusCode shouldBe 200
5620
}
5721

5822
test("should have started wiremock server for second test as well") {
59-
wireMockServer.stubFor(
60-
WireMock.get(WireMock.urlEqualTo("/second-test"))
61-
.willReturn(WireMock.ok())
62-
)
63-
val connection = URL("http://localhost:9000/second-test").openConnection() as HttpURLConnection
64-
connection.responseCode shouldBe 200
23+
wireMockServer.stubOk("/second-test")
24+
25+
val (request, response, result) = "http://localhost:9000/second-test".httpGet().response()
26+
response.statusCode shouldBe 200
6527
}
6628
})
Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,31 @@
11
package io.kotest.extensions.wiremock
22

3+
import com.github.kittinunf.fuel.core.isSuccessful
4+
import com.github.kittinunf.fuel.httpGet
35
import com.github.tomakehurst.wiremock.WireMockServer
4-
import com.github.tomakehurst.wiremock.client.WireMock.*
5-
import io.kotest.assertions.throwables.shouldThrow
6-
import io.kotest.core.listeners.TestListener
76
import io.kotest.core.spec.style.FunSpec
8-
import io.kotest.core.test.TestCase
9-
import io.kotest.core.test.TestResult
107
import io.kotest.matchers.shouldBe
11-
import java.net.ConnectException
12-
import java.net.HttpURLConnection
13-
import java.net.URL
148

15-
@Suppress("BlockingMethodInNonBlockingContext")
169
class WiremockListenerPerTestTest : FunSpec({
1710
val wireMockServer = WireMockServer(9000)
1811

1912
listener(WireMockListener.perTest(wireMockServer))
20-
listener(object : TestListener {
21-
override suspend fun afterTest(testCase: TestCase, result: TestResult) {
22-
shouldThrow<ConnectException> {
23-
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection
24-
connection.responseCode
25-
}
26-
}
27-
})
2813

14+
afterSpec {
15+
val (request, response, result) = "http://localhost:9000/test".httpGet().response()
16+
response.isSuccessful shouldBe false
17+
}
2918

3019
test("should have started wiremock server") {
31-
wireMockServer.stubFor(
32-
get(urlEqualTo("/test"))
33-
.willReturn(ok())
34-
)
35-
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection
36-
connection.responseCode shouldBe 200
20+
wireMockServer.stubOk("/test")
21+
22+
val (request, response, result) = "http://localhost:9000/test".httpGet().response()
23+
response.statusCode shouldBe 200
3724
}
3825

3926
test("should have started wiremock server for second test as well") {
40-
wireMockServer.stubFor(
41-
get(urlEqualTo("/second-test"))
42-
.willReturn(ok())
43-
)
44-
val connection = URL("http://localhost:9000/second-test").openConnection() as HttpURLConnection
45-
connection.responseCode shouldBe 200
27+
wireMockServer.stubOk("/second-test")
28+
val (request, response, result) = "http://localhost:9000/second-test".httpGet().response()
29+
response.statusCode shouldBe 200
4630
}
4731
})

0 commit comments

Comments
 (0)