如何使用黄瓜场景步骤放心地在Get调用中传递查询参数
How to pass query params in Get call with rest-assured using cucumber scenario steps
我正在使用基于 Cucumber 的框架探索放心 (Java)。有一个处理查询参数的方法,它需要一个 的映射,如下所示-
protected Response **getByQueryParams**(final String url, Map<String, ?> queryParams) {
setRootApiUrl();
return given()
.queryParams(queryParams)
.contentType(ContentType.JSON)
.accept(ContentType.JSON).get(url)
.thenReturn();
}
我的获取请求。终点是:** http://localHost:8088/state-names?countryCode=IND**
我有一个黄瓜步骤和下面一样-
Given I perform Get operation for "/state-names" with below query params
|countryCode|
|IND |
我的 Step Def 代码:使用上面的 (getByQueryParams) 方法 >
@Given("I perform Get operation for {string} with below query params")
public void getAllStatesWithCountryCode(String uri,Map<String,String> table) {
var queryParMap=new HashMap<>();
for (Map.Entry<String, String> data : table.entrySet()) {
queryParMap.put(data.getKey(),data.getValue());
}
response = getByQueryParams(uri,queryParMap);
}
这是抛出错误- 所需类型:Map
Provided:HashMap<对象,对象>
错误似乎很明显,但我在 java 方面不是很好,因此无法理解如何通过避免上述错误在我的场景中将此方法与定义的 Map 一起使用。请帮助。
您的 method-getByQueryParams(final String url, Map queryParams) 似乎没问题,因为它旨在针对 String Key 保存任何通用类型的值。
我不确定您的固件形成端点的方式 api url.
但是应该通过声明类型为 的本地地图而不是使用默认 Map<> 即 Map 来解决此错误。
我能够通过以下任一方法修复它:
var queryParMap = new HashMap();
要么
Map queryParMap = new HashMap<>();
希望对您有所帮助。
我正在使用基于 Cucumber 的框架探索放心 (Java)。有一个处理查询参数的方法,它需要一个
protected Response **getByQueryParams**(final String url, Map<String, ?> queryParams) {
setRootApiUrl();
return given()
.queryParams(queryParams)
.contentType(ContentType.JSON)
.accept(ContentType.JSON).get(url)
.thenReturn();
}
我的获取请求。终点是:** http://localHost:8088/state-names?countryCode=IND**
我有一个黄瓜步骤和下面一样-
Given I perform Get operation for "/state-names" with below query params
|countryCode|
|IND |
我的 Step Def 代码:使用上面的 (getByQueryParams) 方法 >
@Given("I perform Get operation for {string} with below query params")
public void getAllStatesWithCountryCode(String uri,Map<String,String> table) {
var queryParMap=new HashMap<>();
for (Map.Entry<String, String> data : table.entrySet()) {
queryParMap.put(data.getKey(),data.getValue());
}
response = getByQueryParams(uri,queryParMap);
}
这是抛出错误- 所需类型:Map
错误似乎很明显,但我在 java 方面不是很好,因此无法理解如何通过避免上述错误在我的场景中将此方法与定义的 Map
您的 method-getByQueryParams(final String url, Map
但是应该通过声明类型为
var queryParMap = new HashMap
希望对您有所帮助。