AndroidDev
AndroidDev

Reputation: 51

Contract Testing using Pact in Android Istrumentation Test

What probably i am trying to do is perform consumer driven contract testing in Android. Here my Android app acts as a consumer and a backend service as a provider. I am trying to use "Pact" framework in my instrumented tests to generate pact file which can be shared with the backend provider to validate the actual contract. Here all i want is to make pact as a mock provider and capture all the "interactions" taking place while running my instrumented tests (Yes Instrumented tests and not local Junit Tests). To my surprise, not a single updated documentation is available on "How to integrate pact framework in Android". All that is available is related to JVM and believe me it is not at all working in Android enviroment. Here is how i am trying to integrate :

Build.gradle:

androidTestImplementation ("au.com.dius.pact.consumer:junit:4.2.9")

This is as per documentation from Pact Official documentation for Consumer. But this dosen't seems to be working at all. I am getting series of errors:

Execution failed for task ':app:mergeDebugAndroidTestNativeLibs'.

Could not resolve all files for configuration ':app:debugAndroidTestRuntimeClasspath'. Failed to transform junit-4.2.9.jar (au.com.dius.pact.consumer:junit:4.2.9) to match attributes {artifactType=android-java-res, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.jvm.version=11, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime, org.jetbrains.kotlin.localToProject=public, org.jetbrains.kotlin.platform.type=jvm}. > Execution failed for JetifyTransform: /Users/sahil/.gradle/caches/modules-2/files-2.1/au.com.dius.pact.consumer/junit/4.2.9/ef8a60f61d128173014aff0797475306511440f1/junit-4.2.9.jar. > Failed to transform '/Users/sahil/.gradle/caches/modules-2/files-2.1/au.com.dius.pact.consumer/junit/4.2.9/ef8a60f61d128173014aff0797475306511440f1/junit-4.2.9.jar' using Jetifier. Reason: null. (Run with --stacktrace for more details.) Failed to transform consumer-4.2.9.jar (au.com.dius.pact:consumer:4.2.9) to match attributes {artifactType=android-java-res, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.jvm.version=11, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime, org.jetbrains.kotlin.localToProject=public, org.jetbrains.kotlin.platform.type=jvm}. > Execution failed for JetifyTransform: /Users/sahil/.gradle/caches/modules-2/files-2.1/au.com.dius.pact/consumer/4.2.9/e01f1240cfae3e5a76119b2b386e1133b2cc0134/consumer-4.2.9.jar.

Can someone please help me in pointing the right direction or a concrete example.

Upvotes: 0

Views: 830

Answers (1)

Matthew Fellows
Matthew Fellows

Reputation: 4065

The reason Pact docs don't mention it, is because there is nothing Android specific about how to do contract testing with Android.

Here all i want is to make pact as a mock provider and capture all the "interactions" taking place while running my instrumented tests (Yes Instrumented tests and not local Junit Tests)

I think you do understand the root of the problem - Pact is a unit testing tool, so it should be used in the context of a unit test and not in the context of an Android environment (test or otherwise).

See also writing consumer tests and using Pact for UI tests as to why what you're proposing is not recommended in general.

Upvotes: 0

Related Questions