Vishal Kukreja
08/01/2022, 3:07 PM@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"
)
)
}
}
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
should_process_the_json_payload_from_providerthis 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
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Boris
08/02/2022, 12:41 AM