``` @HiltAndroidTest @Config(sdk = [Build.VERSION_...
# pact-jvm
v
Copy code
@HiltAndroidTest
@Config(sdk = [Build.VERSION_CODES.Q])
@PactTestFor(
    providerName = "our_provider"
)
@PactDirectory("presentation/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)

  
    lateinit var viewModel: InvestorsProfileViewModel

    init {
        HEADERS["Content-Type"] = "application/json"

        JSON="{\n" +
                " \"score\":45,\n"
                "  \"profile\": \"RISK_AVERSE\"\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<InvestorProfile> = viewModel.getInvestorProfileUseCase.buildUseCaseObservable(
            GetInvestorProfileUseCase.Params.GetInvestorProfileUseCase()).test()
        observer.assertNoErrors()
        observer.assertValue(
            InvestorProfile(
                45,"RISK_AVERSE"
            )
        )
    }
}
m
should_process_the_json_payload_from_provider
this function should call the mock server created by Pact. You aren’t injected the MockServer into that function, whtere you can get the base URL and configure your API client
Here is an example (in Java) that shows how to dynamically configure the API client with the mock server URL: https://github.com/pactflow/example-consumer-java-junit/blob/master/src/test/java/com/example/products/ProductsPactTest.java#L70-L71
For clarity - you must send the request to the Pact mock server, and not the real thing, or mocked out with another framework
b
If all of this isn't making sense, you could try showing us the code you want to test, instead of the test that "doesn't work" 🙂
👍 1