Skip to content

Commit

Permalink
fix: handle exeptions from JUnit 4 state change methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed May 27, 2020
1 parent 08ef85a commit 1d20be8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ open class InteractionRunner<I>(
} 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)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pair<FrameworkMethod, State>>,
Expand All @@ -31,10 +37,15 @@ class RunStateChanges(
val target = stateChangeHandlers.map(Supplier<out Any>::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<*, *>) {
Expand All @@ -43,4 +54,6 @@ class RunStateChanges(
}
}
}

companion object : KLogging()
}

0 comments on commit 1d20be8

Please sign in to comment.