New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using multiple mock providers #117
Comments
The core pact framework does support this, by specifying the consumer and provider with different names. The mock providers will have to run on different ports. I think the base JUnit test and rule have been written with only a single provider in mind. You would need to write your test to interact with the Pact libraries directly in this case. I'll put together an example of using the JUnit DSL with two providers in a test. The Groovy DSL already has an example test with two providers, although it does run them sequentially you can nest the run blocks to have them running at the same time. Have a look at pact-jvm/pact-jvm-consumer-groovy/src/test/groovy/au/com/dius/pact/consumer/groovy/ExampleGroovyConsumerPactTest.groovy |
Thanks, if you can provide an example for JUnit it would be helful. |
I think I got it, although it may still be not very readable, I was missing some verifications on the result. This is what it looks more or less. Mixing the Rule for the first provider and the pact libraries for the second: @Rule
public PactRule rule = new PactRule("localhost", 8080, this);
@Pact(state = "state for provider 1", provider = "provider1", consumer = "consumer")
public PactFragment createFragment(final PactDslWithState builder) {
//return some Fragment
}
public PactFragment createFragmentForProvider2(final PactDslWithProvider builder) {
//return builder
// .given("state for provider 2")
// ....
}
@Test
@PactVerification("state for provider 1")
public void myTest() {
PactFragment fragment = createFragmentForProvider2(ConsumerPactBuilder.consumer("consumer").hasPactWith("provider2"));
MockProviderConfig config = new MockProviderConfig(8081, "localhost");
VerificationResult result = fragment.runConsumer(config, new TestRun() {
public void run(MockProviderConfig config) {
//Test code here
}
});
if (result instanceof PactError) {
throw new RuntimeException(((PactError)result).error());
}
Assert.assertEquals(ConsumerPactTest.PACT_VERIFIED, result);
} |
That's about what I would have done. I might still put together a base test that verifies two providers as an example or update the rule to allow it to be used for multiple providers. |
The JUnit rule now supports multiple providers, have a look at PactMultiProviderTest for an example. |
Will it be available with version 3.0.2? When do you expect the repository will be available? Thanks & regards |
I'll see if I can get 3.0.2 released this weekend |
That would be great! Thanks! |
This works only if you create the PactProviderRule without host and port. If you provide a host and port only one of the Pacts will be considered |
Sry, the reason for that was a host which was not localhost |
Are you saying you resolved the issue by using localhost? Or is this still a problem? |
I configured two provider one on localhost one for a different host. This did not work, it considered only the provider which is not on localhost for the verification. When I changed the second host to localhost both provider where used. |
I have a unit test that interacts with two different providers.
I would like to know if it is possible to generate two different pact files, one for each provider in the same test.
I tried defining two Pact functions, one for each provider, annotated with @ Pact and with the same state as the @ PactVerification test function but it didn't work.
I'm using the PactRule junit rule
By inspecting its code it looks as it scans for all the Pact functions and stores them in a Map with the state as a key, so different pact fragments will be overwritten if they have the same state.
Is there any way of acheiving what I need? Maybe without the Rule approach?
The text was updated successfully, but these errors were encountered: