我在放心地发出 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,如果我可以跳过所有 ""和 {} 会更快。 我的方法正确吗?
最佳答案
让我们看一下您的第一个示例:
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 作为内容类型传递。
那么让我们看看你的第二个例子:
given().parameter("key", "val"). when().post(url + resource).then().assertThat().statusCode(200);
此处您的服务返回 415,因为您缺少 JSON 内容类型。当您将 param
或 parameter
与 POST
一起使用时,您会创建表单参数。表单参数也在请求正文中发送,但表单参数不是 JSON!像您一样将“key”和“val”指定为表单参数将与此相同:
given().contentType("x-www-form-urlencoded").body("key=val").when().url + resource).then().assertThat().statusCode(200);
所以在你的第二个例子中实际上有两个问题:
- 你没有发送 JSON
- 你的内容类型有误
并且因为 (2) 你从服务器得到 415
继续你的第三个例子:
given().parameter("key", "val"). when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));
这里(可能)发生的是您的服务器不包含响应主体,因为它希望请求包含“application/json”作为内容类型。所以没有body to assert(请求错误)!响应仅包含 415 状态(行)作为标题。
这将我们引向您的最后一个示例:
RestAssured.defaultParser = Parser.JSON; given().parameter("key", "val"). when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));
此处您指示 REST Assured 将缺少的内容类型视为 JSON,但问题(再次)是您的服务器根本不返回任何响应主体,因此这无济于事。
解决方案:
您应该在您的类路径中放置一个受支持的 JSON 对象映射框架(Jackson、Faster Jackson、Simple JSON 或 Gson)(例如 jackson-databind
),然后按照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 中创建 map 非常冗长,如果我有嵌套 map ,我通常会这样做:
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 Assured:
MyDTO myDTO = new MyDTO(...);
given().
contentType(ContentType.JSON).
body(myDTO).
when().
post(url + resource).
then().
statusCode(200).
body("otherVal", equalTo(otherVal));
您可以在 object-mapping documentation 中阅读更多内容.
关于java - POST请求失败(放心测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31992532/