2017-05-22 12 views
1

以下のシナリオがなぜ発生するのか理解したいですか? 問題:Pact Junit Ruleを使用すると、JunitテストがHttpConnect例外で失敗します。しかし、私がPact DSL Directlyを使用すると、同じテストに合格し、pactファイルが生成されます。なぜ誰かが私に啓発することができ、Pact Junit Ruleを取得する方法はありますか?Pact JUnitルールの使用とPact DSLの直接使用の比較

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

       } 

直接協定DSLを使用してコード(これはJUnitのは、合格と協定が正常にファイルを生成する)

@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

私は私の注釈を作ることができますJunitのバージョンを4.8から4.9に変更した後で動作します。

関連する問題