• 添加依赖

    <dependency>
      <groupId>au.com.dius</groupId>
      <artifactId>pact-jvm-consumer-junit_2.11</artifactId>
      <version>3.5.10</version>
    </dependency>
    

    契约和测试的编写

    @RunWith(SpringRunner.class)
    @SpringBootTest(properties = {
            "user-service.ribbon.listOfServers: localhost:10001"
    })
    public class UserLoginConsumerPactTest {
        /**
         * 此处起了一个server,用于模拟feign调用的服务
         */
        @Rule
        public PactProviderRuleMk2 stubProvider = new PactProviderRuleMk2("user_provider",
                "127.0.0.1", 10001, this);
    
        @Autowired
        private UserLoginFeignClient userLoginFeignClient;
    
    
        @Autowired
        private OauthService oauthService;
    
        /**
        * 定义pact契约
        */
        @Pact(state = "user login state", provider = "user_provider", consumer = "userLoginFeignClient")
        public RequestResponsePact loginPact(PactDslWithProvider pactDslWithProvider) {
            return pactDslWithProvider
                    .given("user login state")
                    .uponReceiving("user login by name and pass")
                        .path("/login")
                        .matchQuery("name", "alice")
                        .matchQuery("pass", "alice")
                        .method("GET")
                    .willRespondWith()
                        .status(200)
                        .body("{\"code\":1,\"message\":\"name pass valid\",\"data\":\"success\"}",
                                "application/json; charset=UTF-8")
                    .toPact();
        }
    
       /**
        * 验证一下定义pact契约的接口
        */
        @Test
        @PactVerification(fragment = "loginPact")
        public void verifyLoginPact() {
            Result<String> result = userLoginFeignClient.login("alice","alice");
            System.out.println(result);
            Assert.assertEquals(1, result.getCode());
        }
    
        /**
        * 调用feign的service也在这里测试
        */
        @Test
        @PactVerification(fragment = "loginPact")
        public void checkOauthTest() {
            ResponseEntity<Result<String>> responseEntity = oauthService.checkOauth("alice","alice");
            System.out.println(responseEntity.getBody());
            Assert.assertEquals(1, responseEntity.getBody().getCode());
        }
    
        /**
        * 调用feign的service也在这里测试
        */
        @Test
        @PactVerification(fragment = "loginPact")
        public void getOauthJwtTest() {
            ResponseEntity<Result<String>> responseEntity = oauthService.getOauthJwt("alice","alice");
            System.out.println(responseEntity.getBody());
            Assert.assertEquals(1, responseEntity.getBody().getCode());
        }
    }