Skip to content

Commit

Permalink
Reorganize Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoColman committed Jan 7, 2025
1 parent 8aafc3b commit 0b605a2
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 100 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ version = Ci.version
repositories {
mavenCentral()
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots")
}
}

Expand All @@ -23,6 +23,7 @@ dependencies {
implementation(libs.kotest.api)
testImplementation(libs.kotest.runner)
testImplementation(libs.kotest.assertions)
testImplementation(libs.fuel)
}

tasks.test {
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
[versions]
wiremock = "3.6.0"
kotest = "5.5.4"
fuel = "2.3.1"

[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" }

fuel = { module = "com.github.kittinunf.fuel:fuel", version.ref = "fuel" }

wiremock = { module = "org.wiremock:wiremock-standalone", version.ref = "wiremock" }
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import com.github.tomakehurst.wiremock.client.WireMock
import io.kotest.core.config.AbstractProjectConfig

object ProjectConfig : AbstractProjectConfig() {
const val WIREMOCK_SERVER_PORT = 9001
private const val ProjectWiremockServerPort = 9001

val wiremock = WireMockServer(WIREMOCK_SERVER_PORT)
val wiremockListener = WireMockListener(wiremock, ListenerMode.PER_PROJECT)
private val projectWiremock = WireMockServer(ProjectWiremockServerPort)
private val wiremockListener = WireMockListener.perProject(projectWiremock)

override fun extensions() = listOf(wiremockListener)

override suspend fun beforeProject() {
WireMock.configureFor(WIREMOCK_SERVER_PORT)
WireMock.configureFor(ProjectWiremockServerPort)
}
}
19 changes: 19 additions & 0 deletions src/test/kotlin/io/kotest/extensions/wiremock/StubOk.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.kotest.extensions.wiremock

import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.ok
import com.github.tomakehurst.wiremock.client.WireMock.stubFor
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import com.github.tomakehurst.wiremock.stubbing.StubMapping

fun stubOk(url: String) {
stubFor(
get(urlEqualTo(url))
.willReturn(ok())
)
}

fun WireMockServer.stubOk(url: String): StubMapping = stubFor(
get(urlEqualTo(url))
)
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
package io.kotest.extensions.wiremock

import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.ok
import com.github.tomakehurst.wiremock.client.WireMock.stubFor
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import com.github.kittinunf.fuel.httpGet
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.net.HttpURLConnection
import java.net.URL

@Suppress("BlockingMethodInNonBlockingContext")
class WiremockListenerPerProjectTest : FunSpec({
test("should have started wiremock server") {
stubFor(
get(urlEqualTo("/test"))
.willReturn(ok())
)
val connection = URL("http://localhost:9001/test").openConnection() as HttpURLConnection
connection.responseCode shouldBe 200
stubOk("/test")

val (request, response, result) = "http://localhost:9001/test".httpGet().response()
response.statusCode shouldBe 200
}

test("should have started wiremock server for second test as well") {
stubFor(
get(urlEqualTo("/second-test"))
.willReturn(ok())
)
val connection = URL("http://localhost:9001/second-test").openConnection() as HttpURLConnection
connection.responseCode shouldBe 200
stubOk("/second-test")
val (request, response, result) = "http://localhost:9001/second-test".httpGet().response()

response.statusCode shouldBe 200
}
})

Original file line number Diff line number Diff line change
@@ -1,66 +1,28 @@
package io.kotest.extensions.wiremock

import com.github.kittinunf.fuel.httpGet
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.listeners.TestListener
import io.kotest.core.spec.Spec
import io.kotest.core.spec.style.FunSpec
import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import io.kotest.matchers.shouldBe
import java.net.ConnectException
import java.net.HttpURLConnection
import java.net.URL

@Suppress("BlockingMethodInNonBlockingContext")
class WiremockListenerPerSpecTest : FunSpec({
val wireMockServer = WireMockServer(9000)

listener(WireMockListener.perSpec(wireMockServer))

listener(object : TestListener {
override suspend fun afterTest(testCase: TestCase, result: TestResult) {
wireMockServer.stubFor(
WireMock.get(WireMock.urlEqualTo("/after-test"))
.willReturn(WireMock.ok())
)
shouldNotThrowAny {
val connection = URL("http://localhost:9000/after-test").openConnection() as HttpURLConnection
connection.responseCode shouldBe 200
}
}

override suspend fun afterSpec(spec: Spec) {
wireMockServer.stubFor(
WireMock.get(WireMock.urlEqualTo("/after-spec"))
.willReturn(WireMock.ok())
)

shouldThrow<ConnectException> {
val connection = URL("http://localhost:9000/after-spec").openConnection() as HttpURLConnection
connection.responseCode shouldBe 200
}
}
})
test("should have started wiremock server") {
wireMockServer.stubOk("/test")

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

test("should have started wiremock server") {
wireMockServer.stubFor(
WireMock.get(WireMock.urlEqualTo("/test"))
.willReturn(WireMock.ok())
)
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection
connection.responseCode shouldBe 200
response.statusCode shouldBe 200
}

test("should have started wiremock server for second test as well") {
wireMockServer.stubFor(
WireMock.get(WireMock.urlEqualTo("/second-test"))
.willReturn(WireMock.ok())
)
val connection = URL("http://localhost:9000/second-test").openConnection() as HttpURLConnection
connection.responseCode shouldBe 200
wireMockServer.stubOk("/second-test")

val (request, response, result) = "http://localhost:9000/second-test".httpGet().response()
response.statusCode shouldBe 200
}
})
Original file line number Diff line number Diff line change
@@ -1,47 +1,31 @@
package io.kotest.extensions.wiremock

import com.github.kittinunf.fuel.core.isSuccessful
import com.github.kittinunf.fuel.httpGet
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock.*
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.listeners.TestListener
import io.kotest.core.spec.style.FunSpec
import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import io.kotest.matchers.shouldBe
import java.net.ConnectException
import java.net.HttpURLConnection
import java.net.URL

@Suppress("BlockingMethodInNonBlockingContext")
class WiremockListenerPerTestTest : FunSpec({
val wireMockServer = WireMockServer(9000)

listener(WireMockListener.perTest(wireMockServer))
listener(object : TestListener {
override suspend fun afterTest(testCase: TestCase, result: TestResult) {
shouldThrow<ConnectException> {
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection
connection.responseCode
}
}
})

afterSpec {
val (request, response, result) = "http://localhost:9000/test".httpGet().response()
response.isSuccessful shouldBe false
}

test("should have started wiremock server") {
wireMockServer.stubFor(
get(urlEqualTo("/test"))
.willReturn(ok())
)
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection
connection.responseCode shouldBe 200
wireMockServer.stubOk("/test")

val (request, response, result) = "http://localhost:9000/test".httpGet().response()
response.statusCode shouldBe 200
}

test("should have started wiremock server for second test as well") {
wireMockServer.stubFor(
get(urlEqualTo("/second-test"))
.willReturn(ok())
)
val connection = URL("http://localhost:9000/second-test").openConnection() as HttpURLConnection
connection.responseCode shouldBe 200
wireMockServer.stubOk("/second-test")
val (request, response, result) = "http://localhost:9000/second-test".httpGet().response()
response.statusCode shouldBe 200
}
})

0 comments on commit 0b605a2

Please sign in to comment.