Использование правила Pact JUnit по сравнению с прямым использованием Pact DSL?

Я хочу понять, почему происходит следующий сценарий? Проблема: тест Junit завершается с ошибкой с исключением HttpConnect, если я использую правило Pact Junit. Но тот же тест проходит, и файл pact генерируется, если я использую Pact DSL напрямую. Может ли кто-нибудь просветить меня, почему и как начать с правила 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);
    }

person PaChSu    schedule 22.05.2017    source источник


Ответы (1)


Я смог заставить свои аннотации работать после изменения версии Junit с 4.8 на 4.9.

person PaChSu    schedule 06.07.2017