From 3298ac12d4c31e3148055ab7343d10b894419b00 Mon Sep 17 00:00:00 2001 From: Jan Wittrowski Date: Fri, 21 Jun 2024 11:47:01 +0200 Subject: [PATCH] added kotest, improved tests, small code improvements --- src/fibonacci-worker/pom.xml | 8 +++ .../fibonacci_worker/JobExporterService.kt | 8 +-- .../fibonacci_worker/FibonacciKtTest.kt | 11 ++- .../JobExporterServiceTest.kt | 70 +++++-------------- 4 files changed, 32 insertions(+), 65 deletions(-) diff --git a/src/fibonacci-worker/pom.xml b/src/fibonacci-worker/pom.xml index 1345abe..59419a2 100644 --- a/src/fibonacci-worker/pom.xml +++ b/src/fibonacci-worker/pom.xml @@ -18,6 +18,7 @@ 1.9.24 8.5.3 1.12.0 + 5.9.1 @@ -61,6 +62,13 @@ test + + io.kotest + kotest-assertions-core-jvm + ${kotest.version} + test + + diff --git a/src/fibonacci-worker/src/main/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/JobExporterService.kt b/src/fibonacci-worker/src/main/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/JobExporterService.kt index 1fa82e2..49fe868 100644 --- a/src/fibonacci-worker/src/main/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/JobExporterService.kt +++ b/src/fibonacci-worker/src/main/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/JobExporterService.kt @@ -6,16 +6,14 @@ import io.micrometer.core.instrument.MeterRegistry import org.springframework.stereotype.Service import java.util.concurrent.atomic.AtomicInteger -private const val BASE_NAME = "fibonacciworker_jobs" -private const val JOB_KEY_TAG = "jobKey" +internal const val BASE_NAME = "fibonacciworker_jobs" +internal const val JOB_KEY_TAG = "jobKey" @Service class JobExporterService(private val registry: MeterRegistry) { private val jobsInExecution = AtomicInteger(0) - fun getJobsInExecution() = jobsInExecution.get() - init { Gauge.builder("${BASE_NAME}_in_execution") { jobsInExecution.toDouble() @@ -29,6 +27,7 @@ class JobExporterService(private val registry: MeterRegistry) { .tag(JOB_KEY_TAG, jobId) .register(registry) .increment() + jobsInExecution.incrementAndGet() } @@ -38,6 +37,7 @@ class JobExporterService(private val registry: MeterRegistry) { .tag(JOB_KEY_TAG, jobId) .register(registry) .increment() + jobsInExecution.decrementAndGet() } } diff --git a/src/fibonacci-worker/src/test/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/FibonacciKtTest.kt b/src/fibonacci-worker/src/test/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/FibonacciKtTest.kt index df78422..5e62aa2 100644 --- a/src/fibonacci-worker/src/test/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/FibonacciKtTest.kt +++ b/src/fibonacci-worker/src/test/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/FibonacciKtTest.kt @@ -1,17 +1,14 @@ package de.envite.greenbpm.camunda_process_carbon_pricing.fibonacci_worker -import org.junit.jupiter.api.Assertions.* -import org.junit.jupiter.api.Test +import io.kotest.matchers.shouldBe import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments import org.junit.jupiter.params.provider.MethodSource -class FibonacciKtTest{ - @ParameterizedTest(name= "For {0}, the Fibonacci is {1}") +class FibonacciKtTest { + @ParameterizedTest(name = "For {0}, the Fibonacci is {1}") @MethodSource("getData") - fun `should return correct fibonacci values`(input: Int, expected: Int){ - assertEquals(fibonacciRec(input), expected) - } + fun `should return correct fibonacci values`(input: Int, expected: Int) = fibonacciRec(input) shouldBe expected companion object{ @JvmStatic diff --git a/src/fibonacci-worker/src/test/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/JobExporterServiceTest.kt b/src/fibonacci-worker/src/test/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/JobExporterServiceTest.kt index e714acb..e3f22f6 100644 --- a/src/fibonacci-worker/src/test/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/JobExporterServiceTest.kt +++ b/src/fibonacci-worker/src/test/kotlin/de/envite/greenbpm/camunda_process_carbon_pricing/fibonacci_worker/JobExporterServiceTest.kt @@ -1,75 +1,37 @@ package de.envite.greenbpm.camunda_process_carbon_pricing.fibonacci_worker -import io.micrometer.core.instrument.Counter -import io.micrometer.core.instrument.MeterRegistry -import io.mockk.every -import io.mockk.mockk -import io.mockk.verify -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.BeforeEach +import io.kotest.assertions.assertSoftly +import io.kotest.matchers.shouldBe +import io.micrometer.core.instrument.simple.SimpleMeterRegistry import org.junit.jupiter.api.Test -import org.springframework.boot.test.context.SpringBootTest -@SpringBootTest -class JobExporterServiceTest { - private lateinit var meterRegistry: MeterRegistry - private lateinit var classUnderTest: JobExporterService - private lateinit var jobsStartedCounter: Counter - private lateinit var jobsFinishedCounter: Counter - - @BeforeEach - fun setup(){ - meterRegistry = mockk(relaxed = true) - jobsStartedCounter = mockk(relaxed = true) - jobsFinishedCounter = mockk(relaxed = true) - - every { Counter.builder("fibonacciworker_jobs_started") - .tag("jobKey","testId") - .register(meterRegistry) - } returns jobsStartedCounter - every { Counter.builder("fibonacciworker_jobs_finished") - .tag("jobKey","testId") - .register(meterRegistry) - } returns jobsFinishedCounter +class JobExporterServiceTest { - classUnderTest = JobExporterService(meterRegistry) - } + private val meterRegistry: SimpleMeterRegistry = SimpleMeterRegistry() + private val classUnderTest: JobExporterService = JobExporterService(meterRegistry) @Test - fun `reportJobStarted should increase jobsStartedCounter`() { + fun `reportJobStarted should increase jobsStartedCounter and update gauge`() { val id = "testId" classUnderTest.reportJobStarted(id) - verify { jobsStartedCounter.increment() } - - assertEquals(1, classUnderTest.getJobsInExecution()) + assertSoftly { + meterRegistry.get("${BASE_NAME}_started").counter().count() shouldBe 1.0 + meterRegistry.get("${BASE_NAME}_in_execution").gauge().value() shouldBe 1.0 + } } @Test - fun `reportJobFinished should increase jobsFinishedCounter`() { + fun `reportJobStarted should decrease jobsStartedCounter and update gauge`() { val id = "testId" classUnderTest.reportJobFinished(id) - verify { jobsFinishedCounter.increment() } - - assertEquals(-1, classUnderTest.getJobsInExecution()) - - } - - @Test - fun `increase and decrease jobsInExecution`() { - val id = "testId" - - classUnderTest.reportJobStarted(id) - - assertEquals(1, classUnderTest.getJobsInExecution()) - - classUnderTest.reportJobFinished(id) - - assertEquals(0, classUnderTest.getJobsInExecution()) - + assertSoftly { + meterRegistry.get("${BASE_NAME}_finished").counter().count() shouldBe 1.0 + meterRegistry.get("${BASE_NAME}_in_execution").gauge().value() shouldBe -1.0 + } } } \ No newline at end of file