RestAssured - passing list as QueryParam

2019-06-20 04:04发布

I have a REST-service that takes in a number of query-params, amongst other things a list of strings. I use RestAssured to test this REST-service, but I am experiencing some problems with passing the list to the service.

My REST-service:

@GET
@Consumes(Mediatyper.JSON_UTF8)
@Produces(Mediatyper.JSON_UTF8)
public AggregerteDataDTO doSearch(@QueryParam("param1") final String param1,
                                  @QueryParam("param2") final String param2,
                                  @QueryParam("list") final List<String> list) {

My RestAssured test:

public void someTest() {
    final  String url = BASE_URL + "/search?param1=2014&param2=something&list=item1&list=item2";

    final String json = given()
            .expect()
            .statusCode(200)
            .when()
            .get(url)
            .asString();

When I print the url, it looks like this:

http://localhost:9191/application/rest/search?param1=2014&param2=something&list=item1&list=item2

When I try this url in my browser, the REST-service correctly gets a list containing 2 elements. But, when run through my RestAssured-test, only the latter of the params are noticed, giving me a list of 1 element (containing "item2").

2条回答
成全新的幸福
2楼-- · 2019-06-20 04:59

You should upgrade REST Assured to the latest version since I believe that this was a bug in older versions. You could also specify parameters like this:

final String json = 
given().
        param("param1", 2014).
        param("param2", "something").
        param("list", "item1", "item2").
when().
        get("/search").
then().
       statusCode(200).
extract().
          body().asString();  
查看更多
做自己的国王
3楼-- · 2019-06-20 05:06

You can also try the below method

RequestSpecification requestSpecifications = RestAssured.given();

//r.parameters()

Map<String , Object > map = new HashMap<String,Object>();
map.put("param1", 2014);
map.put("param2", "something");
List<String> paramList = new ArrayList<String>();
map.put("list",paramList );

final String json =  requestSpecifications.parameters(map).
    when().
    get("/search").
    then().    
    statusCode(200).
    extract().
    body().asString(); 
查看更多
登录 后发表回答