Hi, I am trying to integrate pact in Android Proje...
# pact-jvm
v
Hi, I am trying to integrate pact in Android Project. while I did not find any sort of documentation for integrating Pact in Android project. Till now what I have done is:-
Copy code
@HiltAndroidTest
@Config(sdk = [Build.VERSION_CODES.Q])
@PactTestFor(
    providerName = "our_provider"
)
@PactFolder("target/pacts")
@ExtendWith(
    PactConsumerTestExt::class)
class VerificationEstimationPactTest {
    private var HEADERS: MutableMap<String, String> = HashMap()
    private var JSON: String? = null


    @get:Rule
    var rule: TestRule = InstantTaskExecutorRule()

    @get:Rule
    var hiltRule = HiltAndroidRule(this)

    @Inject
    lateinit var viewModel: MutualFundVerificationViewModel

    init
    {
        HEADERS["Content-Type"] = "application/json"
        JSON="{\n" +
            "        \"estimationDate\": \"2022-07-26T10:59:29.935Z\",\n"+
            "        \"oEstimationDate\": \"2022-07-27T10:59:56.342Z\",\n"+
            "        \"estimationDate\": \"2022-07-27T11:12:26.640Z\",\n"+
            "        \"gstimationDate\": \null \n"+
            " }"

    }

    @Before
    fun setUp() {
       hiltRule.inject()
    }

    @Throws(
        UnsupportedEncodingException::class
    )
    @Pact(provider = "our_provider", consumer = "our_consumer")
    fun createFragment(builder: PactDslWithProvider): RequestResponsePact {
        return builder
            .given("data count is > 0")
            .uponReceiving("a request for json data")
            .path("/provider.json")
            .method("GET")
            .willRespondWith()
            .status(200)
            .headers(HEADERS)
            .body(JSON)
            .toPact()
    }

    @Test
    @PactTestFor(pactMethod = "createFragment")
    fun should_process_the_json_payload_from_provider() {
//        val observer: TestObserver<VerificationTimeResponse> = viewModel.flowUseCase.getVerificationTime().test()
//        observer.assertNoErrors()
        /*observer.assertValue(
            VerificationTimeResponse(
                Timestamp("2022-07-26T10:59:29.935Z".toLong()),
                Timestamp("2022-07-27T10:59:56.342Z".toLong()),
                Timestamp("2022-07-27T11:12:26.640Z".toLong()),
                null
            )
        )*/
    }
still unable to generate the contract file. Kindly help for same.
m
I can’t advise about the above, but I will say there should be nothing specific about an Android Pact test. You do need to run the tests as a pure unit test, and not run in any emulator/android environment
💯 1
b
Yeah, it's hard to see how the provider is being invoked in this code. • If you're making API calls directly from your Fragment, that's already a sign of questionable encapsulation. • Something that seems missing is injection of the Pact stub service URL into the HTTP client.