Skip to content

Commit

Permalink
fix: If the JUnit test framework has an exception, add a failure to t…
Browse files Browse the repository at this point in the history
…he test results #1715
  • Loading branch information
rholshausen committed Apr 22, 2024
1 parent 02a0d56 commit e16aea8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,6 +48,7 @@ class PactVerificationExtensionSpec extends Specification {
@Shared ClassicHttpRequest classicHttpRequest
@Shared ProviderVerifier verifier
@Shared RequestDataToBeVerified data
@Shared Optional<Throwable> executionException

def setupSpec() {
verifier = Mock(ProviderVerifier)
Expand All @@ -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())
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e16aea8

Please sign in to comment.