You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pact JVM 4.6.3:如何用新API替代Legacy DSL构建V4Pact?

Pact JVM 4.6.3 新API契约定义示例(Kotlin/Java)

针对你提供的旧Legacy DSL代码,以下是改用Pact JVM新API的实现方式:

Kotlin 实现

import au.com.dius.pact.core.model.V4Pact
import au.com.dius.pact.provider.junit5.Pact
import au.com.dius.pact.core.model.RequestMethod
import au.com.dius.pact.core.model.ContentType
import static au.com.dius.pact.core.model.Response.body

@Pact(provider = "ArticlesProvider", consumer = "ArticlesConsumer")
fun createPact(builder: PactBuilder): V4Pact {
    return builder
        .interaction("ListAllArticles") {
            given("Provider with three articles")
            request {
                method = RequestMethod.GET
                path = "/articles.json"
            }
            response {
                status = 200
                body = body(
                    """
                    {
                      "articles" : [ {
                        "name" : "Basic news",
                        "length" : 104
                      }, {
                        "name" : "About dogs",
                        "length" : 5684
                      }, {
                        "name" : "Gossip",
                        "length" : 4512384
                      } ]
                    }
                    """.trimIndent(),
                    ContentType.JSON
                )
            }
        }
        .build(V4Pact::class.java)
}

Java 实现(类似逻辑)

import au.com.dius.pact.core.model.V4Pact;
import au.com.dius.pact.provider.junit5.Pact;
import au.com.dius.pact.core.model.RequestMethod;
import au.com.dius.pact.core.model.ContentType;
import static au.com.dius.pact.core.model.Response.body;

@Pact(provider = "ArticlesProvider", consumer = "ArticlesConsumer")
public V4Pact createPact(PactBuilder builder) {
    return builder
        .interaction("ListAllArticles", interaction -> {
            interaction.given("Provider with three articles");
            interaction.request(request -> {
                request.method(RequestMethod.GET);
                request.path("/articles.json");
            });
            interaction.response(response -> {
                response.status(200);
                response.body(body("""
                    {
                      "articles" : [ {
                        "name" : "Basic news",
                        "length" : 104
                      }, {
                        "name" : "About dogs",
                        "length" : 5684
                      }, {
                        "name" : "Gossip",
                        "length" : 4512384
                      } ]
                    }
                    """.trimIndent(), ContentType.JSON));
            });
        })
        .build(V4Pact.class);
}

关键变化说明

  • 移除了usingLegacyDsl()调用,直接采用新的结构化DSL语法
  • 用interaction()方法封装整个交互逻辑,参数对应原代码的uponReceiving交互名称
  • 前置状态通过given()直接定义,与原逻辑一致
  • 请求细节在request代码块内配置,通过属性赋值(Kotlin)或方法调用(Java)指定请求方法和路径
  • 响应细节在response代码块内配置,状态码直接赋值,响应体通过body()方法传入JSON字符串并指定ContentType.JSON,确保契约的内容类型元数据正确
  • 最后通过build()方法生成V4Pact实例,替代原代码的toPact()

内容的提问来源于stack exchange,提问作者bugs_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:57:45