json POST请求失败(放心测试)

lnvxswe2  于 5个月前  发布在  其他
关注(0)|答案(3)|浏览(85)

我有问题,使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,如果我可以跳过所有的“”和{},那会更快。我的方法正确吗?

ht4b089n

ht4b089n1#

让我们看看你的第一个例子:

given().contentType(ContentType.JSON).body("{\"key\": \"val\"}").    
        when().post(url + resource).then().assertThat().statusCode(200).body("otherVal", equalTo(otherVal));

字符串
这里发生的事情是你把{ "key" : "val" }(作为文本)放入请求的主体。这个文本恰好是JSON。从REST Assured的Angular 来看,你也可以把{ "key" : "val"放入,这不是有效的JSON。你的服务器正确响应,因为 server 需要并理解JSON。它理解主体应该是JSON,因为你把JSON作为内容类型传递。
让我们看看你的第二个例子:

given().parameter("key", "val").                                      
        when().post(url + resource).then().assertThat().statusCode(200);


这里你的服务返回415,因为你缺少JSON内容类型。当你使用paramparameterPOST时,你会创建表单参数。表单参数也会在请求体中发送,但表单参数不是JSON!像你一样,将“key”和“瓦尔”作为表单参数将是相同的:

given().contentType("x-www-form-urlencoded").body("key=val").when().url + resource).then().assertThat().statusCode(200);


所以在你的第二个例子中,实际上有两个问题:
1.你不发送JSON
1.你有错误的内容类型
因为(2)你从服务器得到415
继续你的第三个例子:

given().parameter("key", "val").                                                         
            when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));


这里(可能)发生的情况是,您的服务器不包含响应主体,因为它期望请求包含“application/json”作为内容类型。因此没有主体可以Assert(请求是错误的)!响应仅包含415状态(行)作为头部。
这就引出了你的最后一个例子:

RestAssured.defaultParser = Parser.JSON;                                                   
given().parameter("key", "val").                                                       
        when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));


在这里,您指示REST Assured将缺少的内容类型视为JSON,但问题(再次)是您的服务器根本不返回任何响应主体,因此这不会有帮助。

解决方案:

你应该在你的类路径中放置一个受支持的JSON对象Map框架(Jackson、FasterJackson、Simple JSON或Gson)(例如jackson-databind),然后按照文档中的描述创建一个Map:

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));


您可以在对象Map文档中阅读更多内容。

nsc4cvqm

nsc4cvqm2#

我在寻找答案,我也找到了。
添加一个文件到你的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"));

字符串

hk8txs48

hk8txs483#

如问题所述“无法序列化对象,因为在类路径中找不到JSON序列化程序”,请添加对Jackson数据绑定https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind的依赖性

相关问题