pact-consumer测试中各个参数的模式匹配

时间:2017-04-06 22:36:44

标签: pact pact-java

使用pact-jvm - Java

所以我们的api用几个参数的十进制/浮点值来响应。 " body":{ "状态":" api已启动。", " totalTime":0.005939006805419922 }" 我尝试使用正则表达式匹配,但是pact body生成数据,这与实际api返回的小数不匹配。

package pact;
import au.com.dius.pact.consumer.dsl.DslPart;
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.model.PactFragment;
import au.com.dius.pact.consumer.ConsumerPactTest;
import java.util.Map;
import java.util.HashMap;
import au.com.dius.pact.consumer.PactProviderRule;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import org.junit.Rule;
import au.com.dius.pact.consumer.dsl.PactDslJsonArray;

public class PactTest extends ConsumerPactTest {

    @Rule
    public PactProviderRule mockProvider = new PactProviderRule("test_provider", "localhost", 1234, this);
    String v3Path = "/v3";
    private DslPart body = new PactDslJsonBody()
            .stringType("status", "api is up.")
            .decimalType("totalTime", 0.005939006805419922);

    protected PactFragment createFragment(PactDslWithProvider builder) {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");

        PactFragment fragment = builder
                .uponReceiving("response")
                .path(v3Path)
                .method("GET")
                .willRespondWith()
                .status(200)
                .headers(headers)
                .body(body)
                .toFragment();
        return fragment;
    }

    @Override
    protected String providerName() {
        return "test_provider";
    }

    @Override
    protected String consumerName() {
        return "test_consumer";
    }

    @Override
    protected void runTest(String url) {
        Map response;
        try {
            response = new ConsumerClient(url).getAsMap(v3Path, "");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

生成的协议:

{
"provider": {
    "name": "test_provider"
},
"consumer": {
    "name": "test_consumer"
},
"interactions": [
    {
        "description": "API v3 endpoint response",
        "request": {
            "method": "GET",
            "path": "/v3"
        },
        "response": {
            "status": 200,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": {
                "status": "api is up.",
                "totalTime": 0.005939006805419922
            },
            "matchingRules": {
                "body": {
                    "$.status": {
                        "matchers": [
                            {
                                "match": "type"
                            }
                        ]
                    },
                    "$.totalTime": {
                        "matchers": [
                            {
                                "match": "decimal"
                            }
                        ]
                    }
                }
            }
        }
    }
],
"metadata": {
    "pact-specification": {
        "version": "3.0.0"
    },
    "pact-jvm": {
        "version": "3.5.0-beta.2"
    }
}

}

协议的差异与实际的反应:

0) Verifying a pact between test_consumer and test_provider - API v3 endpoint response returns a response which has a matching body
  $.body.totalTime -> Expected 0.005939006805419922 but received 0.00545501708984375

差异:

  @1
      "status": "api is up.",
  -    "totalTime": 0.005939006805419922
  +    "totalTime": 0.00545501708984375
  }

所以可以做一个&#34; eachlike&#34;而不是decimalType来匹配这些值的模式?当我查看eachLike并且它接受一个字符串和一个int - https://github.com/DiUS/pact-jvm/blob/master/pact-jvm-consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslJsonBody.java#L580

1 个答案:

答案 0 :(得分:1)

看起来您在使用者测试中使用的Pact库版本与用于验证提供程序的版本之间存在版本不匹配。

您的消费者测试使用的是pact库的V3(3.5.0-beta.2),并且正在生成V3匹配表达式($.totalTime)。

当针对提供者验证协议时,它期望V2表达式($.body.totalTime),然后使用相等进行匹配,因为它认为该属性没有匹配器。

如果您在消费者测试中降级到版本3.3.7,它将默认为V2。或者你可以通过在测试类中添加以下内容来强制V2并仍然使用3.5.0-beta.2:

@Override
protected PactSpecVersion getSpecificationVersion() {
    return PactSpecVersion.V2;
}