Search code examples
javajunitpact

Using Pact JUnit Rule versus Using the Pact DSL directly?


I want to understand why is the below scenario occurring? Issue : The Junit test fails with HttpConnect exception if I use Pact Junit Rule. But the same test passes and pact file is generated if I use the Pact DSL Directly. Can someone enlighten me why and how to get going with the Pact Junit Rule?

Code using Pact Junit Rule :(This fails with HttpHostConnectException)

@Rule
     public PactProviderRule rule = new PactProviderRule("DrivePOC", PactSpecVersion.V2, this);

     /*Setting up what your expectations are*/
        @Pact(provider = "P1",consumer = "C1")
        public PactFragment createFragment(PactDslWithProvider builder)
        {

            PactFragment pactFragment = ConsumerPactBuilder
                    .consumer("C1")
                    .hasPactWith("P1")
                    .uponReceiving("load Files Data")
                        .path("/loadData")
                        .method("POST")
                        .body("{\"givefileId\": \"abc\"}")
                    .willRespondWith()
                        .status(200)
                        .body("{\"fileId\": \"abcfileId1234\"}")
                        .toFragment();
            return pactFragment;

        }

        /*Test similar to Junit, verifies if the test are ok and the responses are as per expectations set in the createFragment*/
        @Test
        @PactVerification(value = "P1")
        public void runTest() throws IOException
        {
            MockProviderConfig config = MockProviderConfig.createDefault();
            Map expectedResponse = new HashMap();
            expectedResponse.put("fileId", "abcfileId1234");
            try {
                Assert.assertEquals(new ProviderClient(config.url()).helloToDrive("{\"givefileId\": \"abc\"}"),
                        expectedResponse);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

              }

Code using Pact DSL Directly(This Junit passes and generate Pact file successfully)

@Test
        public void testPact() {
            PactFragment pactFragment = ConsumerPactBuilder
                .consumer("C1")
                .hasPactWith("P1")
                .uponReceiving("load Files Data")
                    .path("/loadData")
                    .method("POST")
                    .body("{\"givefileId\": \"abc\"}")
                .willRespondWith()
                    .status(200)
                    .body("{\"fileId\": \"abcfileId1234\"}")
                    .toFragment();

            MockProviderConfig config = MockProviderConfig.createDefault();
            VerificationResult result = pactFragment.runConsumer(config, new TestRun() {
                public void run(MockProviderConfig config) throws Throwable {
                    Map expectedResponse = new HashMap();
                    expectedResponse.put("fileId", "abcfileId1234");
                    try {
                        Assert.assertEquals(new ProviderClient(config.url()).helloToHueDrive("{\"givefileId\": \"abc\"}"),
                                expectedResponse);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });

            if (result instanceof PactError) {
                throw new RuntimeException(((PactError)result).error());
            }

            Assert.assertEquals(ConsumerPactTest.PACT_VERIFIED, result);
    }

Solution

  • I could make my annotations work, after changing Junit version from 4.8 to 4.9.