From 89d6bf29e1cb894717398a31d4ca2c5f8debb7bf Mon Sep 17 00:00:00 2001 From: nbrouand <7816908+nbrouand@users.noreply.github.com> Date: Thu, 19 Dec 2024 11:31:30 +0100 Subject: [PATCH] chore(dsl): Refacto exception constructor (#44) --- .../ChutneyScenarioExecutionContext.kt | 6 ++-- .../junit/engine/execution/ExecutionUtil.kt | 9 ++++-- ...esolvedScenarioEnvironmentExceptionTest.kt | 30 +++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 kotlin-dsl/src/test/kotlin/com/chutneytesting/kotlin/junit/engine/execution/UnresolvedScenarioEnvironmentExceptionTest.kt diff --git a/kotlin-dsl/src/main/kotlin/com/chutneytesting/kotlin/junit/engine/execution/ChutneyScenarioExecutionContext.kt b/kotlin-dsl/src/main/kotlin/com/chutneytesting/kotlin/junit/engine/execution/ChutneyScenarioExecutionContext.kt index dbeb9b2ba..7353bd213 100644 --- a/kotlin-dsl/src/main/kotlin/com/chutneytesting/kotlin/junit/engine/execution/ChutneyScenarioExecutionContext.kt +++ b/kotlin-dsl/src/main/kotlin/com/chutneytesting/kotlin/junit/engine/execution/ChutneyScenarioExecutionContext.kt @@ -181,9 +181,9 @@ class ChutneyScenarioExecutionContext( private fun convertExecuteException(t: Throwable, scenarioDescriptor: ChutneyScenarioDescriptor): Throwable { return when (t) { - is UnresolvedEnvironmentException -> UnresolvedScenarioEnvironmentException(t.message + " Please, specify a name or declare only one environment.") - is NoEnvironmentFoundException -> UnresolvedScenarioEnvironmentException(t.message + " Please, declare one.") - is EnvironmentNotFoundException -> UnresolvedScenarioEnvironmentException("Environment [${scenarioDescriptor.environmentName}] not found. ${t.message}") + is UnresolvedEnvironmentException -> UnresolvedScenarioEnvironmentException(t) + is NoEnvironmentFoundException -> UnresolvedScenarioEnvironmentException(t) + is EnvironmentNotFoundException -> UnresolvedScenarioEnvironmentException(t, scenarioDescriptor.environmentName) else -> AssertionError(t) } } diff --git a/kotlin-dsl/src/main/kotlin/com/chutneytesting/kotlin/junit/engine/execution/ExecutionUtil.kt b/kotlin-dsl/src/main/kotlin/com/chutneytesting/kotlin/junit/engine/execution/ExecutionUtil.kt index 709da893c..6ae6961c0 100644 --- a/kotlin-dsl/src/main/kotlin/com/chutneytesting/kotlin/junit/engine/execution/ExecutionUtil.kt +++ b/kotlin-dsl/src/main/kotlin/com/chutneytesting/kotlin/junit/engine/execution/ExecutionUtil.kt @@ -24,8 +24,13 @@ class StepExecutionFailedException(step: Step) : }" ) -class UnresolvedScenarioEnvironmentException(message: String?) : - NoStackTraceAssertionError(message ?: "Cannot resolve environment") +class UnresolvedScenarioEnvironmentException( + throwable: Throwable, + environmentName: String? = null +) : NoStackTraceAssertionError( + environmentName?.let { "${throwable.message}: Environment [$it] not found." } + ?: "${throwable.message}: Please, specify a name or declare only one environment." +) fun Step.findSubStepPath(toBeFound: Step): List { if (this == toBeFound) { diff --git a/kotlin-dsl/src/test/kotlin/com/chutneytesting/kotlin/junit/engine/execution/UnresolvedScenarioEnvironmentExceptionTest.kt b/kotlin-dsl/src/test/kotlin/com/chutneytesting/kotlin/junit/engine/execution/UnresolvedScenarioEnvironmentExceptionTest.kt new file mode 100644 index 000000000..afa368d5b --- /dev/null +++ b/kotlin-dsl/src/test/kotlin/com/chutneytesting/kotlin/junit/engine/execution/UnresolvedScenarioEnvironmentExceptionTest.kt @@ -0,0 +1,30 @@ +/* + * SPDX-FileCopyrightText: 2017-2024 Enedis + * + * SPDX-License-Identifier: Apache-2.0 + * + */ + +package com.chutneytesting.kotlin.junit.engine.execution + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class UnresolvedScenarioEnvironmentExceptionTest { + + @Test + fun exceptionMessageWithEnvironmentName() { + val throwable = Throwable("Error occurred") + val exception = UnresolvedScenarioEnvironmentException(throwable, "test-environment") + + assertEquals("Error occurred: Environment [test-environment] not found.", exception.message) + } + + @Test + fun exceptionMessageWithoutEnvironmentName() { + val throwable = Throwable("Error occurred") + val exception = UnresolvedScenarioEnvironmentException(throwable) + + assertEquals("Error occurred: Please, specify a name or declare only one environment.", exception.message) + } +}