From b215e023d74206db4ff89d9048389f89f8f95d7e Mon Sep 17 00:00:00 2001 From: houlisha <16525973+HarisHoulis@users.noreply.github.com> Date: Mon, 12 Feb 2024 08:48:28 +0100 Subject: [PATCH] Assert with Strikt --- build.gradle | 1 + .../http/ReportHttpTransactionTests.kt | 63 +++++++++++-------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/build.gradle b/build.gradle index d47e2eea..a92d6eab 100644 --- a/build.gradle +++ b/build.gradle @@ -37,6 +37,7 @@ dependencies { testImplementation "org.http4k:http4k-testing-hamkrest" testImplementation("org.jetbrains.kotlin:kotlin-test") + testImplementation("io.strikt:strikt-core:0.34.1") } group = 'com.gildedrose' diff --git a/src/test/kotlin/com/gildedrose/http/ReportHttpTransactionTests.kt b/src/test/kotlin/com/gildedrose/http/ReportHttpTransactionTests.kt index 5152c381..f1314594 100644 --- a/src/test/kotlin/com/gildedrose/http/ReportHttpTransactionTests.kt +++ b/src/test/kotlin/com/gildedrose/http/ReportHttpTransactionTests.kt @@ -1,14 +1,18 @@ package com.gildedrose.http -import com.gildedrose.com.gildedrose.testing.assertAll -import org.http4k.core.Method +import org.http4k.core.Method.GET import org.http4k.core.Request import org.http4k.core.Response -import org.http4k.core.Status +import org.http4k.core.Status.Companion.OK import org.http4k.core.then import org.junit.jupiter.api.Test +import strikt.api.expectThat +import strikt.assertions.hasSize +import strikt.assertions.isA +import strikt.assertions.isEqualTo +import strikt.assertions.withElementAt +import strikt.assertions.withFirst import java.time.Duration -import kotlin.test.assertEquals internal class ReportHttpTransactionTests { @@ -20,33 +24,40 @@ internal class ReportHttpTransactionTests { @Test fun `reports transactions to analytics`() { filter.then { - Response(Status.OK) - }.invoke(Request(Method.GET, "/")) - assertAll( - events.single() as HttpEvent, - { assertEquals("/", uri) }, - { assertEquals(Method.GET.toString(), method) }, - { assertEquals(Status.OK.code, status) } - ) + Response(OK) + }.invoke(Request(GET, "/")) + expectThat(events) + .hasSize(1) + .withFirst { + isA().and { + get(HttpEvent::uri).isEqualTo("/") + get(HttpEvent::method).isEqualTo(GET.toString()) + get(HttpEvent::status).isEqualTo(OK.code) + } + } } @Test fun `reports slow transactions to analytics with an additional event`() { filter.then { Thread.sleep(25) - Response(Status.OK) - }.invoke(Request(Method.GET, "/")) - assertAll( - events.first() as HttpEvent, - { assertEquals("/", uri) }, - { assertEquals(Method.GET.toString(), method) }, - { assertEquals(Status.OK.code, status) } - ) - assertAll( - events[1] as SlowHttpEvent, - { assertEquals("/", uri) }, - { assertEquals(Method.GET.toString(), method) }, - { assertEquals(Status.OK.code, status) } - ) + Response(OK) + }.invoke(Request(GET, "/")) + expectThat(events) + .hasSize(2) + .withFirst { + isA().and { + get(HttpEvent::uri).isEqualTo("/") + get(HttpEvent::method).isEqualTo(GET.toString()) + get(HttpEvent::status).isEqualTo(OK.code) + } + } + .withElementAt(1) { + isA().and { + get(SlowHttpEvent::uri).isEqualTo("/") + get(SlowHttpEvent::method).isEqualTo(GET.toString()) + get(SlowHttpEvent::status).isEqualTo(OK.code) + } + } } }