From 1d20be82c1ef8d941f48801be6f062e02147f6fe Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Thu, 21 May 2020 16:04:33 +1000 Subject: [PATCH] fix: handle exeptions from JUnit 4 state change methods --- .../pact/provider/junit/InteractionRunner.kt | 1 + .../pact/provider/junit/RunStateChanges.kt | 21 +++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/provider/junit/src/main/kotlin/au/com/dius/pact/provider/junit/InteractionRunner.kt b/provider/junit/src/main/kotlin/au/com/dius/pact/provider/junit/InteractionRunner.kt index add42a8980..f6a52a7ee7 100644 --- a/provider/junit/src/main/kotlin/au/com/dius/pact/provider/junit/InteractionRunner.kt +++ b/provider/junit/src/main/kotlin/au/com/dius/pact/provider/junit/InteractionRunner.kt @@ -157,6 +157,7 @@ open class InteractionRunner( } catch (e: Throwable) { if (!pending) { notifier.fireTestFailure(Failure(description, e)) + notifier.fireTestFinished(description) } testResult = VerificationResult.Failed(listOf(mapOf("message" to "Request to provider failed with an exception", "exception" to e)), diff --git a/provider/junit/src/main/kotlin/au/com/dius/pact/provider/junit/RunStateChanges.kt b/provider/junit/src/main/kotlin/au/com/dius/pact/provider/junit/RunStateChanges.kt index e174612975..6992ea3ef8 100644 --- a/provider/junit/src/main/kotlin/au/com/dius/pact/provider/junit/RunStateChanges.kt +++ b/provider/junit/src/main/kotlin/au/com/dius/pact/provider/junit/RunStateChanges.kt @@ -3,11 +3,17 @@ package au.com.dius.pact.provider.junit import au.com.dius.pact.core.model.ProviderState import au.com.dius.pact.provider.junitsupport.State import au.com.dius.pact.provider.junitsupport.StateChangeAction +import mu.KLogging import org.junit.runners.model.FrameworkMethod import org.junit.runners.model.Statement import java.util.function.Supplier import kotlin.reflect.full.isSubclassOf +data class StateChangeCallbackFailed( + override val message: String, + override val cause: Throwable +) : Exception(message, cause) + class RunStateChanges( private val next: Statement, private val methods: List>, @@ -31,10 +37,15 @@ class RunStateChanges( val target = stateChangeHandlers.map(Supplier::get).find { it::class.isSubclassOf(method.first.declaringClass.kotlin) } - val stateChangeValue = if (method.first.method.parameterCount == 1) { - method.first.invokeExplosively(target, providerState.params) - } else { - method.first.invokeExplosively(target) + val stateChangeValue = try { + if (method.first.method.parameterCount == 1) { + method.first.invokeExplosively(target, providerState.params) + } else { + method.first.invokeExplosively(target) + } + } catch (e: Throwable) { + logger.error(e) { "State change method for \"${providerState.name}\" failed" } + throw StateChangeCallbackFailed("State change method for \"${providerState.name}\" failed", e) } if (stateChangeValue is Map<*, *>) { @@ -43,4 +54,6 @@ class RunStateChanges( } } } + + companion object : KLogging() }