如何在使用Cucumber场景步骤中使用rest-assured传递Get调用中的查询参数

huangapple go评论79阅读模式
英文:

How to pass query params in Get call with rest-assured using cucumber scenario steps

问题

我正在使用基于Cucumber的rest-assuredJava框架进行探索已经编写了一个处理查询参数的方法它期望一个如下的<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&lt;String, ?&gt; 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 &quot;/state-names&quot; with below query params
|countryCode|
|IND        |

My Step Def code: using above (getByQueryParams) method >

@Given(&quot;I perform Get operation for {string} with below query params&quot;)
public void getAllStatesWithCountryCode(String uri,Map&lt;String,String&gt; table) {
var queryParMap=new HashMap&lt;&gt;();
for (Map.Entry&lt;String, String&gt; 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.

huangapple
  • 本文由 发表于 2020年10月22日 15:09:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64477012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定