使用Pact JUnit规则与直接使用Pact DSL?

时间:2017-05-22 09:00:20

标签: java junit pact

我想了解为什么会出现以下情况? 问题:如果我使用Pact Junit规则,Junit测试将失败并显示HttpConnect异常。但是,如果我直接使用Pact DSL,则会生成相同的测试并生成pact文件。有人可以告诉我为什么以及如何使用Pact Junit规则?

使用Pact Junit规则的代码:(这与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);
            }

              }

直接使用Pact DSL的代码(此Junit成功传递并生成Pact文件)

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

1 个答案:

答案 0 :(得分:1)

在将Junit版本从4.8更改为4.9之后,我可以使我的注释工作。