我建议将重点放在对 API 的调用上,而不是填充不是您真正想要测试的 POJO 结构。POST 只不过是在 http 调用中发送字符串,因此要编写测试,您需要设置正确的字符串以发送到 API。
正如您提出的问题,您可以通过创建/生成 Java 客户端然后从 wiki 填充其(复杂)模型来进行调用来做到这一点。另一种方法(我推荐,并创建了通用的固定装置来支持)是创建一个正文模板以发布并使用 wiki 填充该模板中的占位符。然后不需要(API)特定的 Java 代码,换句话说,没有 POJO 可以填充。
模板可以完全在 wiki 中,使用 Slim 场景(如果要填充一组固定的占位符),也可以在使用模板引擎填充的单独文件中(如果需要更大的灵活性,例如可选占位符或可变长度列表)。在后一种情况下,为模板引擎提供占位符的数据结构只是一个带有键和值的哈希/映射(而不是带有 getter 和 setter 的 pojo)。在前一种情况下,占位符作为参数提供给场景。
基于场景
使用场景生成两个请求,邮政编码占位符:
!*> Scenario definition
!define POST_BODY_2 { {{{
<s11:Envelope xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/">
<s11:Body>
<ns1:GetCityWeatherByZIP xmlns:ns1="http://ws.cdyne.com/WeatherWS/">
<ns1:ZIP>@{zip}</ns1:ZIP>
</ns1:GetCityWeatherByZIP>
</s11:Body>
</s11:Envelope>
}}} }
|script|xml http test|
|scenario |send request _|zip, City |
|post |${POST_BODY_2} |to |${URL} |
|check |response status|200 |
|show |response |
|register prefix|weather |for namespace |http://ws.cdyne.com/WeatherWS/|
|check |xPath |//weather:City/text()|@{City} |
*!
|send request |
|zip |City |
|10007|New York |
|94102|San Francisco|

自由标记模板
通过使用 Freemarker 模板,我们可以拥有更多动态的正文内容(并且使用 'set value for ' 方法填充占位符)。使用此模板 (samplePost.ftl.xml):
<s11:Envelope xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/">
<s11:Body>
<ns1:GetWeather xmlns:ns1="http://www.webserviceX.NET">
<#if cityName??>
<ns1:CityName>${cityName}</ns1:CityName>
</#if>
<ns1:CountryName>${countryName}</ns1:CountryName>
</ns1:GetWeather>
</s11:Body>
</s11:Envelope>
可以创建以下 wiki:
!define GLOBAL_WEATHER_URL {http://www.webservicex.com/globalweather.asmx}
!define GLOBAL_WEATHER_TEMPLATE_NAME {samplePost.ftl.xml}
!3 Don't send a cityName element: error
The Freemarker template will not send a cityName element if no value is supplied (the whole element will be omitted, as can be seen in the request below). The service will not like this.
Omitting the element on a null value could not be done when the body was in the scenario content.
|script |xml http test |
|template |${GLOBAL_WEATHER_TEMPLATE_NAME} |
|set value |http://www.webserviceX.NET/GetWeather|for header|SOAPAction |
|set value |Canada |for |countryName|
|post template to|${GLOBAL_WEATHER_URL} |
|check |response status |500 |
|show |request |
|show |response |
!3 Send a cityName element: success
When a cityName value is set the enclosing element is sent by the Freemarker template (as visible in the shown request content below), and the SOAP call will succeed.
|script |xml http test |
|template |${GLOBAL_WEATHER_TEMPLATE_NAME} |
|set value |http://www.webserviceX.NET/GetWeather |for header |SOAPAction |
|set value |Canada |for |countryName |
|set value |Vancouver International Air-Port, B. C.|for |cityName |
|post template to|${GLOBAL_WEATHER_URL} |
|check |response status |200 |
|show |request |
|show |response |
|register prefix |wsX |for namespace|http://www.webserviceX.NET|
|show |xPath |!-//wsX:GetWeatherResult/text()-! |

这两个示例发布 XML,但同样的方法当然也可以用于 JSON API(然后我建议使用'json http test' 夹具,它支持 json 路径而不是 xPath 来对收到的响应进行断言)。
当需要更复杂的占位符(例如可变长度的列表)或者您希望将占位符分组到多个嵌套哈希中(就像您在 POJO 模型中处理嵌套对象一样),这也可以通过使用更详细的键名来实现(例如|set value|someone@example.com|for|email.to[1]|
)
上面使用的固定装置可以从 GitHub 下载,也可以通过 Maven 使用:
<dependency>
<groupId>nl.hsac</groupId>
<artifactId>hsac-fitnesse-fixtures</artifactId>
<version>2.7.1</version>
</dependency>
您可以尝试这种方法是否适用于您的 API,只需下载项目的“独立 zip”版本、解压缩、运行它,然后更新示例 wiki 页面之一,以使用更新的模板发布到 API 的 URL 并执行页面/测试。