不确定发生了什么,完整的错误是:
Problem with i/o No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )
我正在尝试向 RESTful 服务发送 PUT 请求。我正在解析 POST 并为 PUT 发送“ID”和“启用”的修改后的键/值对。
@Test(dependsOnMethods= {"Post"})
public void Put() throws IOException {
logger.logTestActivity("Testing FORM data POST using Excel file");
requestBuilder = new RequestSpecBuilder();
String jsonString = "";
boolean enabledModify = false;
try {
BufferedReader in = new BufferedReader(new FileReader(
xmlTest.getParameter("json-post")));
String str;
while ((str = in.readLine()) != null) {
jsonString += str;
}
JSONObject jsonObjPut = new JSONObject(jsonString);
jsonObjPut.put("_id", createUserId);
jsonObjPut.put("enabled",enabledModify);
System.out.println(jsonObjPut.toString());
in.close();
requestBuilder.setContentType(ContentType.JSON);
requestSpecification = requestBuilder.build();
responseBuilder.expectStatusCode(Integer.parseInt(xmlTest
.getParameter("http-status-code-200")));
responseBuilder.expectContentType(ContentType.JSON);
responseSpecification = responseBuilder.build();
System.out.println(createUserId);
String responseJson = given().body(jsonObjPut).
when().put("/" + createUserId).asString();
System.out.println(responseJson);
logger.logTestActivity("Finished testing FORM data POST using Excel file");
} catch (AssertionError e ) {
logger.logTestActivity(
"Error testing FORM data post: " + e.getMessage(),logger.ERROR);
System.out.println("REST URL: " + RestAssured.baseURI + " "
+ RestAssured.port + " " + RestAssured.basePath );
Assert.fail("Error testing FORM data POST: " + e.getMessage());
throw e;
} catch (IOException | JSONException e) {
System.out.println("Problem with i/o" + e.getMessage());
}
}
createUserID 是一个全局变量,是从 POST 中解析出的 ID。
正在解析的 JSON 文件如下所示:
{
"enabled" : false,
"_id" : "fdse332a-22432d-4432b"
}
在之前的测试方法中,我正在使用所有适当的 url 端点设置放心...
此外,PUT 也失败,并出现 NULLPointerException 错误。这可能是将来的另一篇文章!
解决方案:在传递给 resassured 时,我没有将 JSON 对象转换为字符串。
String responseJson = given().body(jsonObjPut.toString).
这成功了。我现在使用生成的 ID 修改现有的 json,并在 RESTful 服务上成功执行 PUT。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)