Junit PACT DSL.closeObject格式分层,PACT文件交互,响应
我无法使用PACT DSL.closeObject格式化PACT交互响应。我正在征求建议以使此工作正常,或者询问.closeObject是否未按预期工作?我有一个购物车,里面有两样东西。当我尝试使用.closeObject格式化预期的响应时,它将不会编译,包含2项,请参见下面的代码。编译错误出现在iPhone行.stringMatchername之后的第一个.closeObject上。我需要在PACT文件中创建shoppingCartItems的层次结构。PACT DSL.closeObject的广告用法可以在地图中匹配任意键部分的此链接中找到Junit PACT DSL.closeObject格式分层,PACT文件交互,响应,junit,pact,pact-java,Junit,Pact,Pact Java,我无法使用PACT DSL.closeObject格式化PACT交互响应。我正在征求建议以使此工作正常,或者询问.closeObject是否未按预期工作?我有一个购物车,里面有两样东西。当我尝试使用.closeObject格式化预期的响应时,它将不会编译,包含2项,请参见下面的代码。编译错误出现在iPhone行.stringMatchername之后的第一个.closeObject上。我需要在PACT文件中创建shoppingCartItems的层次结构。PACT DSL.closeObject
预期的JSON响应负载应该类似于,这里是与示例JSON匹配的经过更正和注释的代码
private DslPart respSc6() {
DslPart body = new PactDslJsonBody()
.stringMatcher("id", "ShoppingCart_[0-9]*", "ShoppingCart_0")
.eachLike("shoppingCartItem") // Starts an array [1] and an object [2] (like calling .object(...)) and applies it to all items
.numberValue("quantity", 1)
.stringMatcher("state", "new") // You are using a simple string as the regex here, so it will only match 'new'
.object("productOffering") // Start a new object [3]
.stringMatcher("id", "IPHONE_7") // Again, this regex will only match 'IPHONE_7'
.stringMatcher("name", "iPhone") // Again, this regex will only match 'iPhone'
.closeObject() // Close the object started in [3]
.closeObject() // Close the object started in [2]
.closeArray(); // Close the array started in [1]
return body;
}
您不需要为shoppingCartItem数组提供两个示例对象定义,因为.eachLike matcher设计为将一个定义应用于数组中的所有项目。如果希望生成的示例JSON包含两个项,请将数字2作为第二个参数传入,例如,..eachLikeshoppingCartItem,2。编译错误消息到底是什么?看起来您可能关闭对象的次数太多了?此外,您有一个结束closeArray,但从未在任何地方开始数组。我建议您改用JSON正文字符串匹配器,因为它比使用DSL创建对象要简单一些。
private DslPart respSc6() {
DslPart body = new PactDslJsonBody()
.stringMatcher("id", "ShoppingCart_[0-9]*", "ShoppingCart_0")
.eachLike("shoppingCartItem") // Starts an array [1] and an object [2] (like calling .object(...)) and applies it to all items
.numberValue("quantity", 1)
.stringMatcher("state", "new") // You are using a simple string as the regex here, so it will only match 'new'
.object("productOffering") // Start a new object [3]
.stringMatcher("id", "IPHONE_7") // Again, this regex will only match 'IPHONE_7'
.stringMatcher("name", "iPhone") // Again, this regex will only match 'iPhone'
.closeObject() // Close the object started in [3]
.closeObject() // Close the object started in [2]
.closeArray(); // Close the array started in [1]
return body;
}