我在使用rest-assured发出POST请求时遇到了问题。
下面的代码可以工作:
given().contentType(ContentType.JSON).body("{\"key\": \"val\"}").
when().post(url + resource).then().assertThat().statusCode(200).body("otherVal", equalTo(otherVal)); 但我尝试使用的是param()或parameter()方法:
这一条提供了:
given().parameter("key", "val").
when().post(url + resource).then().assertThat().statusCode(200); Expected status code <200> doesn't match actual status code <415>.
这一点:
given().parameter("key", "val").
when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal)); java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but no content-type was defined in the response. Try registering a default parser using: RestAssured.defaultParser(<parser type>);
还有这个:
RestAssured.defaultParser = Parser.JSON;
given().parameter("key", "val").
when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));java.lang.IllegalArgumentException: The JSON input text should neither be null nor empty
我已经想不出什么是错的了。
我想要做的是避免为所有的测试编写完整的json,如果我可以跳过所有的"“和{},它会更快。我的方法正确吗?
发布于 2015-08-15 20:31:23
让我们看一下你的第一个例子:
given().contentType(ContentType.JSON).body("{\"key\":\"val\"}")。when().post(url + resource).then().assertThat().statusCode(200).body("otherVal",equalTo(otherVal));
这里发生的事情是您将{ "key" : "val" } (作为文本)放入请求的主体中。这个文本恰好是JSON。从REST Assured的角度来看,您也可以将{ "key" : "val"放入无效的JSON。您的服务器可以正确响应,因为服务器需要并理解JSON。它理解主体应该是JSON,因为您将JSON作为content-type传递。
让我们看一下你的第二个例子:
给定
().parameter(“key”,"val")。when().post(url +url
这里,您的服务返回415,因为您缺少JSON content-type。在POST中使用param或parameter时,会出现创建表单参数的情况。表单参数也在请求正文中发送,但表单参数不是JSON!像您这样指定"key“和"val”作为表单参数将与如下所示相同:
given().contentType("x-www-form-urlencoded").body("key=val").when().url + resource).then().assertThat().statusCode(200);所以在你的第二个例子中,实际上有两个问题:
因为(2)你从服务器得到415
继续看你的第三个例子:
给定
().parameter(“key”,"val")。when().post(url + resource).then().assertThat().body("otherVal",equalTo(otherVal));
这里发生的情况(可能)是您的服务器不包含响应主体,因为它希望请求包含"application/json“作为content-type。所以没有正文可以断言(请求是错误的)!响应只包含415状态(行)作为报头。
这将我们引向您的最后一个示例:
RestAssured.defaultParser = Parser.JSON;给定().parameter(“key”,"val")。when().post(url + resource).then().assertThat().body("otherVal",equalTo(otherVal));
在这里,您指示REST Assured将缺少的内容类型作为JSON处理,但是问题(还是)是您的服务器根本不返回任何响应体,所以这不会有帮助。
解决方案:
您应该在类路径(例如jackson-databind)中放置一个受支持的JSON对象映射框架(Jackson、Faster Jackson、Simple JSON或Gson),然后按照documentation中的描述创建一个映射
Map<String, Object> jsonAsMap = new HashMap<>();
map.put("key", "val");
given().
contentType(ContentType.JSON).
body(jsonAsMap).
when().
post(url + resource).
then().
statusCode(200).
body("otherVal", equalTo(otherVal)); 因为在Java中创建映射非常繁琐,所以如果有嵌套的映射,我通常会这样做:
given().
contentType(ContentType.JSON).
body(new HashMap<String,Object>() {{
put("key1, "val1");
put("key2, "val2");
put("key3", asList("val3", "val4"));
put("nested", new HashMap<String,String>() {{
put("key4", "val4");
put("key5", "val5");
}});
}}).
when().
post(url + resource).
then().
statusCode(200).
body("otherVal", equalTo(otherVal)); 或者,您可以创建数据的DTO表示,然后将一个对象传递给REST:
MyDTO myDTO = new MyDTO(...);
given().
contentType(ContentType.JSON).
body(myDTO).
when().
post(url + resource).
then().
statusCode(200).
body("otherVal", equalTo(otherVal)); 您可以在object-mapping documentation中阅读更多内容。
发布于 2016-06-24 04:44:42
我一直在寻找答案,我也找到了答案..
将文件添加到src/ test /resouces文件夹,并将此代码添加到您的测试中。一切都应该很好
URL file = Resources.getResource("ModyNewFieldsReq.json");
String myRequest = Resources.toString(file,Charsets.UTF_8);
Response fieldResponse = given ()
.header("Authorization", AuthrztionValue)
.header("X-App-Client-Id", XappClintIDvalue)
.contentType("application/vnd.api+json")
.body(myRequest).with()
.when()
.post(dataPostUrl)
.then()
.assertThat()
.log().ifError()
.statusCode(200)
.extract().response();
Assert.assertFalse(fieldResponse.asString().contains("isError"));https://stackoverflow.com/questions/31992532
复制相似问题