英文:
How to pass query params in Get call with rest-assured using cucumber scenario steps
问题
我正在使用基于Cucumber的rest-assured(Java)框架进行探索。已经编写了一个处理查询参数的方法,它期望一个如下的<String, ?>的映射-
protected Response **getByQueryParams**(final String url, Map<String, ?> queryParams) {
setRootApiUrl();
return given()
.queryParams(queryParams)
.contentType(ContentType.JSON)
.accept(ContentType.JSON).get(url)
.thenReturn();
}
我的Get请求端点是:**http://localHost:8088/state-names?countryCode=IND**
**我有一个对应的Cucumber步骤,如下-**
Given 使用以下查询参数执行Get操作 "/state-names"
|countryCode|
|IND |
**我的步骤定义代码:使用上述(getByQueryParams)方法 >**
@Given("我执行带有以下查询参数的Get操作{string}")
public void 使用国家代码获取所有状态(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<String, ?>
提供的类型:HashMap<Object, Object>
错误似乎很明显,但我在Java方面不太擅长,所以无法理解如何在我的场景中使用定义的Map<String, ?>来使用这个方法,以避免上述错误。请帮忙。
英文:
I'm exploring rest-assured (Java) with cucumber based Framework. There is a method written to handle query parameters, it expects a map of<String,?> as below-
protected Response **getByQueryParams**(final String url, Map<String, ?> queryParams) {
setRootApiUrl();
return given()
.queryParams(queryParams)
.contentType(ContentType.JSON)
.accept(ContentType.JSON).get(url)
.thenReturn();
}
My Get Req. End point is:** http://localHost:8088/state-names?countryCode=IND**
I've a cucumber step for same as below-
Given I perform Get operation for "/state-names" with below query params
|countryCode|
|IND |
My Step Def code: using above (getByQueryParams) method >
@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);
}
this is throwing an error- Required type: Map<String,?>
Provided:HashMap<Object,Object>
Error seems obvious but I'm not that good in java so unable to understand how to use this method with defined Map<String,?> in my scenario by avoiding above error. Plz help.
答案1
得分: 1
你的方法getByQueryParams(final String url, Map<String, ?> queryParams)
看起来是没问题的,因为它被设计成可以保存任意通用类型的值与字符串键对应。
我不太确定你的框架是如何构建端点 API URL 的。
但是这个错误应该通过声明你的本地映射为<String, String>
类型来解决,而不是使用默认的Map<>
,即Map<Obj,Obj>
。
我成功修复了这个问题,可以使用以下任一方式:
var queryParMap = new HashMap<String, String>();
或者
Map<String, String> queryParMap = new HashMap<>();
希望对你有所帮助。
英文:
Your method-getByQueryParams(final String url, Map<String, ?> queryParams) seems to be fine as this designed to hold any generic type of value against String Key.
I'm not sure about the way your FW forming the endpoint api url.
But this error should be resolved by declaring your local map of type <String, String> rather than using default- Map<> i.e. Map<Obj,Obj>.
I was able to get it fixed with either of below:
var queryParMap = new HashMap<String,String>();
or
Map<String, String> queryParMap = new HashMap<>();
Hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论