Skip to content
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

Closed
sbruno opened this issue Apr 30, 2015 · 12 comments
Closed

Using multiple mock providers #117

sbruno opened this issue Apr 30, 2015 · 12 comments
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@sbruno
Copy link

sbruno commented Apr 30, 2015

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?

@uglyog
Copy link
Member

uglyog commented May 1, 2015

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

@sbruno
Copy link
Author

sbruno commented May 4, 2015

Thanks, if you can provide an example for JUnit it would be helful.
I was actually trying to create a second PactFragment and doing runConsumer with the second provider configuration. There I was trying to nest the run blocks but I didn't know how to check for the test result, so the test always passed. Also the resulting code was not very readable.

@sbruno
Copy link
Author

sbruno commented May 4, 2015

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);
    }

@uglyog
Copy link
Member

uglyog commented May 6, 2015

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.

@uglyog
Copy link
Member

uglyog commented Aug 22, 2015

The JUnit rule now supports multiple providers, have a look at PactMultiProviderTest for an example.

@uglyog uglyog closed this as completed Aug 22, 2015
@PatrikSteuer
Copy link

Will it be available with version 3.0.2? When do you expect the repository will be available?

Thanks & regards
Patrik

@uglyog
Copy link
Member

uglyog commented Aug 28, 2015

I'll see if I can get 3.0.2 released this weekend

@PatrikSteuer
Copy link

That would be great! Thanks!

@PatrikSteuer
Copy link

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

@PatrikSteuer
Copy link

Sry, the reason for that was a host which was not localhost

@uglyog
Copy link
Member

uglyog commented Sep 2, 2015

Are you saying you resolved the issue by using localhost? Or is this still a problem?

@PatrikSteuer
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants