diff --git a/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactVerificationExtension.kt b/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactVerificationExtension.kt index b055853f4..8c6f999c8 100644 --- a/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactVerificationExtension.kt +++ b/provider/junit5/src/main/kotlin/au/com/dius/pact/provider/junit5/PactVerificationExtension.kt @@ -17,6 +17,8 @@ import au.com.dius.pact.provider.ProviderVerifier import au.com.dius.pact.provider.RequestData import au.com.dius.pact.provider.RequestDataToBeVerified import au.com.dius.pact.provider.TestResultAccumulator +import au.com.dius.pact.provider.VerificationFailureType +import au.com.dius.pact.provider.VerificationResult import au.com.dius.pact.provider.junitsupport.VerificationReports import au.com.dius.pact.provider.reporters.ReporterManager import io.pact.plugins.jvm.core.InteractionVerificationData @@ -211,10 +213,26 @@ open class PactVerificationExtension( val store = context.getStore(ExtensionContext.Namespace.create("pact-jvm")) val testContext = store.get("interactionContext") as PactVerificationContext val pact = if (this.pact is FilteredPact) pact.pact else pact - val updateTestResult = testResultAccumulator.updateTestResult(pact, interaction, testContext.testExecutionResult, - pactSource, propertyResolver) - if (updateTestResult is Result.Err) { - throw AssertionError("Failed to update the test results: " + updateTestResult.error.joinToString("\n")) + if (context.executionException.isPresent) { + val e = context.executionException.get() + val failure = VerificationResult.Failed("Test method has failed with an exception: ${e.message}", + failures = mapOf( + interaction.interactionId.orEmpty() to + listOf(VerificationFailureType.ExceptionFailure("Test method has failed with an exception", e)) + ) + ) + testResultAccumulator.updateTestResult( + pact, interaction, testContext.testExecutionResult + failure, + pactSource, propertyResolver + ) + } else { + val updateTestResult = testResultAccumulator.updateTestResult( + pact, interaction, testContext.testExecutionResult, + pactSource, propertyResolver + ) + if (updateTestResult is Result.Err) { + throw AssertionError("Failed to update the test results: " + updateTestResult.error.joinToString("\n")) + } } } diff --git a/provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/PactVerificationExtensionSpec.groovy b/provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/PactVerificationExtensionSpec.groovy index 646709c61..c56b648a4 100644 --- a/provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/PactVerificationExtensionSpec.groovy +++ b/provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/PactVerificationExtensionSpec.groovy @@ -20,6 +20,7 @@ import au.com.dius.pact.provider.ProviderVerifier import au.com.dius.pact.provider.RequestData import au.com.dius.pact.provider.RequestDataToBeVerified import au.com.dius.pact.provider.TestResultAccumulator +import au.com.dius.pact.provider.VerificationResult import org.apache.hc.core5.http.ClassicHttpRequest import org.apache.hc.core5.http.HttpRequest import org.junit.jupiter.api.extension.ExtensionContext @@ -47,6 +48,7 @@ class PactVerificationExtensionSpec extends Specification { @Shared ClassicHttpRequest classicHttpRequest @Shared ProviderVerifier verifier @Shared RequestDataToBeVerified data + @Shared Optional executionException def setupSpec() { verifier = Mock(ProviderVerifier) @@ -61,8 +63,10 @@ class PactVerificationExtensionSpec extends Specification { put(_, _) >> { args -> contextMap[args[0]] = args[1] } } + executionException = Optional.empty() extContext = Stub { getStore(_) >> store + getExecutionException() >> { executionException } } interaction1 = new RequestResponseInteraction('interaction1', [], new Request(), new Response()) interaction2 = new RequestResponseInteraction('interaction2', [], new Request(), new Response()) @@ -130,6 +134,28 @@ class PactVerificationExtensionSpec extends Specification { exception.message == 'Failed to update the test results: failed' } + @Issue('#1715') + def 'If the JUnit test framework has an exception, add a failure to the test results'() { + given: + pact = new RequestResponsePact(new Provider(), new Consumer(), [interaction1, interaction2], [:], pactSource) + + extension = new PactVerificationExtension(pact, pactSource, interaction1, + 'service', 'consumer', mockValueResolver) + extension.testResultAccumulator = Mock(TestResultAccumulator) + + executionException = Optional.of(new RuntimeException('No test result for you')) + + when: + extension.afterTestExecution(extContext) + + then: + 1 * extension.testResultAccumulator.updateTestResult(pact, interaction1, { + def result = it[0] + result instanceof VerificationResult.Failed && + result.description == 'Test method has failed with an exception: No test result for you' + }, pactSource, mockValueResolver) + } + @Issue('#1572') def 'beforeEach method passes the property resolver on to the verification context'() { given: