我有多个API测试,它们有不同的多部分输入值集,这些值动态变化,我想创建一个公共函数,它可以将这些变化的多个多部分接受到一个单一的多部分行中,这样我就不必创建多个响应函数来接受这些变化的值。
public test1(){
| KEY| VALUE |
|key1 | value1|
|key2 | value2|
|key3 | value3|
}
public test2(){
| KEY| VALUE |
|key1 | value1|
|key2 | value2|
}
public test3(){
| KEY| VALUE |
|key1 | value1|
|key2 | value2|
|key3 | value3|
|key4 | value4|
|key5 | value5|
}
实际:
public void common() {
Response response = given().urlEncodingEnabled(true).config(RestAssured.config()
.encoderConfig(encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.TEXT)))
.multiPart("key1","value1")
.multiPart("key2","value2")
.multiPart("key3","value3")
.multiPart("key4","value4")
.multiPart("key5","value5")
.config(RestAssured.config()
.contentType(ContentType.JSON)
.headers(request.getHeaders())
.post(path);
}
预期逻辑类似于将所有多部分值组合到单个函数中:
public void common() {
given().urlEncodingEnabled(true).config(RestAssured.config()
.encoderConfig(encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.TEXT)))
.multiPart("key1","value1";"key2","value2";"key3","value3";"key4","value4";"key5","value5")
.config(RestAssured.config()
.contentType(ContentType.JSON)
.headers(request.getHeaders())
.post(path);
}
任何指针将不胜感激。
2条答案
按热度按时间o3imoua41#
类似下面的东西应该与您的用例相匹配,您可以添加一个类似buildMultiParts的帮助器方法,它将接受类型为string的var args并返回添加了multipart的修改后的请求
您还可以看到添加了参数的输出
oxf4rvwz2#
这里有两个解决方案(从我的观点来看):
1.写一个自定义函数,参数是一个
Map<String,?>
,然后为每个Map的EntrySet添加多个multipart
。1.第二种方法与第一种方法相同,但这次使用
varargs
来获取动态参数。但缺点是key
和value
的数据类型必须是String。它不像第一个解决方案那样是动态的。