当前位置: 首页 > 知识库问答 >
问题:

pact-jvm:如何解决au.com.dius.pact.consumer.PactMisMatchesException

裴嘉良
2023-03-14

我正在尝试使用Pact-JVM在我们的两个服务之间生成一个协议。但是当我尝试运行Java类时,我得到了这个异常。

au.com.dius.pact.consumer.PactMismatchesException: The following requests were not received:
method: GET
path: /devices/v1
query: [externalId:[0942dc67-35de-44f7-a061-743f59436a98]]
headers: [:]
matchers: MatchingRules(rules=[:])
generators: Generators(categories={})
body: OptionalBody(state=MISSING, value=null)
public class PactForDevice {
    Map<String, String> headers = MapUtils.putAll(new HashMap<String, String>(), new String[]{"Content-Type", "application/json;charset=UTF-8"});

@Rule
public PactProviderRuleMk2 provider = new PactProviderRuleMk2("device-service-m", this);

@Pact(consumer = "device-r", provider = "device-service-m")
public RequestResponsePact createFragment(PactDslWithProvider builder) {

    return builder
            .given("Device M details")
            .uponReceiving("retrieving Device details")
            .path("/devices/v1")
            .method("GET")
            .query("externalId=0942dc67-35de-44f7-a061-743f59436a98")
            .willRespondWith()
            .headers(headers)
            .status(200)
            .body("{" +
                    "\"data\": [,\n " +
                    "{ \n" +
                    " \"dateRegistered\": \"2017-07-13T11:10:51.000+12:00\",\n" +
                    " \"alias\": \"\",\n" +
                    " \"id\": \"a02b14ee72192ab3\",\n" +
                    " \"description\": \"Samsung SM-G930F\",\n" +
                    " \"title\": \"a02b14ee72192ab3\",\n" +
                    " \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
                    "},\n" +
                    "{\n" +
                    " \"dateRegistered\": \"2017-07-13T10:45:51.000+12:00\",\n" +
                    " \"alias\": \"\",\n" +
                    " \"id\": \"a41c3af56ec35874\",\n" +
                    " \"description\": \"Samsung SM-T819\",\n" +
                    " \"title\": \"a41c3af56ec35874\",\n" +
                    " \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
                    " },\n" +
                    " {\n" +
                    " \"dateRegistered\": \"2017-07-13T10:45:31.000+12:00\",\n" +
                    " \"alias\": \"\",\n" +
                    " \"id\": \"bd2b027bbd0a2f17\",\n" +
                    " \"description\": \"Samsung SM-A320Y\",\n" +
                    " \"title\": \"bd2b027bbd0a2f17\",\n" +
                    " \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
                    " }\n" +
                    "],\n" +
                    " \"message\": \"3 devices found for the user 0942dc67-35de-44f7-a061-743f59436a98\"\n" +
                    "}")
            .toPact();
}

@PactVerification("device-service-m")
@Test
@JsonIgnoreProperties(ignoreUnknown = true)
public void runTest1() throws IOException {

    final GetDevicesResponse deviceResponse = new GetDevicesResponse();

    final List<Device> deviceList = new ArrayList<>();
    Device dev = new Device();
    dev.withDateRegistered("2017-07-13T11:10:51.000+12:00");
    dev.withAlias("");
    dev.withId("a02b14ee72192ab3");
    dev.withDescription("Samsung SM-G930F");
    dev.withTitle("a02b14ee72192ab3");
    dev.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
    deviceList.add(dev);

    Device dev1 = new Device();
    dev1.withDateRegistered("2017-07-13T10:45:51.000+12:00");
    dev1.withAlias("");
    dev1.withId("a41c3af56ec35874");
    dev1.withDescription("Samsung SM-T819");
    dev1.withTitle("a41c3af56ec35874");
    dev1.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
    deviceList.add(dev1);

    Device dev2 = new Device();
    dev2.withDateRegistered("2017-07-13T10:45:31.000+12:00");
    dev2.withAlias("");
    dev2.withId("bd2b027bbd0a2f17");
    dev2.withDescription("Samsung SM-A320Y");
    dev2.withTitle("bd2b027bbd0a2f17");
    dev2.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
    deviceList.add(dev2);

    deviceResponse.setDevices(deviceList);

    final RestTemplate restTemplate = new RestTemplate();
    GetDevicesResponse devices = restTemplate.getForObject("http://localhost:8091/devices/v1?externalId=0942dc67-35de-44f7-a061-743f59436a98", GetDevicesResponse.class);

    assertThat(devices, sameBeanAs(deviceResponse));

}  

共有1个答案

司寇飞航
2023-03-14

我有一个类似的问题,即在测试运行后没有生成协议。我从未使用注释方法使它们工作,而是通过扩展ConsumerPactTestMK2解决了它。Pact将设置mockserver并为您模拟响应。

public class PactForDevice extends ConsumerPactTestMk2 {
    Map<String, String> headers = MapUtils.putAll(new HashMap<String, String>(), new String[]{"Content-Type", "application/json;charset=UTF-8"});

    public RequestResponsePact createPact(PactDslWithProvider builder) {
        return builder
          .given("Device M details")
          .uponReceiving("retrieving Device details")
          .path("/devices/v1")
          .method("GET")
          .query("externalId=0942dc67-35de-44f7-a061-743f59436a98")
          .willRespondWith()
          .headers(headers)
          .status(200)
          .body("{" +
                "\"data\": [,\n " +
                "{ \n" +
                " \"dateRegistered\": \"2017-07-13T11:10:51.000+12:00\",\n" +
                " \"alias\": \"\",\n" +
                " \"id\": \"a02b14ee72192ab3\",\n" +
                " \"description\": \"Samsung SM-G930F\",\n" +
                " \"title\": \"a02b14ee72192ab3\",\n" +
                " \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
                "},\n" +
                "{\n" +
                " \"dateRegistered\": \"2017-07-13T10:45:51.000+12:00\",\n" +
                " \"alias\": \"\",\n" +
                " \"id\": \"a41c3af56ec35874\",\n" +
                " \"description\": \"Samsung SM-T819\",\n" +
                " \"title\": \"a41c3af56ec35874\",\n" +
                " \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
                " },\n" +
                " {\n" +
                " \"dateRegistered\": \"2017-07-13T10:45:31.000+12:00\",\n" +
                " \"alias\": \"\",\n" +
                " \"id\": \"bd2b027bbd0a2f17\",\n" +
                " \"description\": \"Samsung SM-A320Y\",\n" +
                " \"title\": \"bd2b027bbd0a2f17\",\n" +
                " \"externalId\": \"0942dc67-35de-44f7-a061-743f59436a98\"\n" +
                " }\n" +
                "],\n" +
                " \"message\": \"3 devices found for the user 0942dc67-35de-44f7-a061-743f59436a98\"\n" +
                "}")
        .toPact();
    }

    @Override
    protected String providerName() {
        return "device-service-m";
    }

    @Override
    protected String consumerName() {
        return "device-r";
    }

    @Override
    protected void runTest(MockServer mockServer) throws IOException {
        final GetDevicesResponse deviceResponse = new GetDevicesResponse();

        final List<Device> deviceList = new ArrayList<>();
        Device dev = new Device();
        dev.withDateRegistered("2017-07-13T11:10:51.000+12:00");
        dev.withAlias("");
        dev.withId("a02b14ee72192ab3");
        dev.withDescription("Samsung SM-G930F");
        dev.withTitle("a02b14ee72192ab3");
        dev.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
        deviceList.add(dev);

        Device dev1 = new Device();
        dev1.withDateRegistered("2017-07-13T10:45:51.000+12:00");
        dev1.withAlias("");
        dev1.withId("a41c3af56ec35874");
        dev1.withDescription("Samsung SM-T819");
        dev1.withTitle("a41c3af56ec35874");
        dev1.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
        deviceList.add(dev1);

        Device dev2 = new Device();
        dev2.withDateRegistered("2017-07-13T10:45:31.000+12:00");
        dev2.withAlias("");
        dev2.withId("bd2b027bbd0a2f17");
        dev2.withDescription("Samsung SM-A320Y");
        dev2.withTitle("bd2b027bbd0a2f17");
        dev2.withExternalId("0942dc67-35de-44f7-a061-743f59436a98");
        deviceList.add(dev2);

        deviceResponse.setDevices(deviceList);

        String url = mockServer.getUrl();
        String path = "devices/v1";
        String query = "externalId=0942dc67-35de-44f7-a061-743f59436a98";

        URIBuilder uriBuilder = null;
        try {
            uriBuilder = new URIBuilder(url)
                                .setPath(path)
                                .setQuery(query);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        GetDevicesResponse devices = new ObjectMapper().readValue(Request.Get(uriBuilder.toString())
                .addHeader("content-type", "application/json")
                .execute().returnContent().asString(), GetDevicesResponse.class);
        assertThat(devices, sameBeanAs(deviceResponse));
    }
}

使用这种方法,我必须将谷歌番石榴19添加到我的POM中。但效果不错。

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>19.0</version>
</dependency>
 类似资料:
  • 我在pact-jvm-consumer中的closeArray有问题。 给定这样的Json,如何构造“dslPart imeiResults=new PactDslJsonBody()”-语句。 我试过这样: 但这不起作用,例如.closearray()不返回PactDslJsonBody,而是返回DslPart,所以在.closearray()之后永远不能有任何东西?我不明白,有人能给我们演示一

  • 所以我有一个包含一些请求和响应数据的json文件,我想要完成的是遍历这些数据并创建一个使用每个请求和响应的pact文件。 所以目前我正在使用junit中的参数化测试来迭代我们的json数据,这基本上是有效的,只是因为生产者名称对于所有pacts都是相同的,它创建了相同的文件并覆盖了前面的文件。 不确定是否有更好的方法来实现这一点,我查看了Github for Pact Jvm并查看了堆栈溢出,但未

  • 我有一些关于Pact JVM(Java)的问题; > 如果提供者的json结构响应已知(例如通过使用Postman查询api),是否可以将Postman json主体自动转换为pact契约文件,而不需要运行(java)使用者片段创建方法,或者例如将其转换为java PactDslJsonBody()结构? 在创建pact文件时,是否需要启动并运行提供程序(spring-boot)(即使json响应

  • 使用带有spring boot的pact jvm DSL(.timestamp(名称、格式、示例)),我可以指定以下两种: 如果您发送日期dd-MM-yyyy而不是yyyy-MM-dd,我的真正的提供者服务就会出错,但是我不明白当使用者端的测试错误地将格式错误的JSON时间戳发送给提供者API时,您应该如何使测试失败。 即。如下所示的位:

  • 我有一个java的Spring BootAPI,它使用pact-jvm进行pact验证。我们有一个新的客户机,他想使用一个新的路径来使用相同的API,网关会处理这个路径,但这会引起pacts的问题,我想截取请求,并修改请求的路径,使新的pacts指向旧的路径。我试图在网上查阅一些资料,发现:https://medium.com/dazn-tech/pact-contract-testing-dea