无法在控制台上打印api响应

时间:2017-06-04 07:37:58

标签: java eclipse rest-assured testng-dataprovider

出于某种原因,当我尝试使用print语句为响应主体打印响应时,系统不会打印。请帮忙。

在下面的API帖子中,我在Eclipse Neon 3上使用Java,rest-assured,TestNG。使用@DataProvider注释,我在Post请求中传递多个params以查看调用的响应。任何帮助都会得到真正的赞赏。

package com.auto.restassured;

import io.restassured.RestAssured;
import static io.restassured.RestAssured.basic;
import static io.restassured.RestAssured.given;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import io.restassured.response.Response;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;


public class FilePostToVirusTotal {


static String baseURL = "https://www.virustotal.com/vtapi/v2/file/report";
Response myResponse;


@DataProvider(name = "md5hashes")
public String[][] createMd5Hashes() {

    return new String[][]  {

        {"md51", "c1105fb75bc00b5e487f7b26a5be7088"},
        {"md52", "213f3287c81d09b095334c9f3151cff8"},
        {"md53", "b00c2c458b4cf1eb172e354f54f0fe12"},
        {"md54", "32ac9b6b6b7cdbfce179acc5edae98c3"},
        {"md55", "510b0b81b85c025d538ed4bad78dc64f"},

    };

}

@Test(dataProvider = "md5hashes")
public void md5JsonTest(String apikey, String resource)
{

    //Catch API response
    myResponse = given().param("text", resource).param("text", "34b937e6e2d28ee6f93a70392d958de8ac4a8dd842e08bbca9bcb0d22f9b9960").when().post(baseURL);
    //Print Response
    System.out.println(myResponse.getBody().asString());

}

}

2 个答案:

答案 0 :(得分:2)

您可以使用内置日志方法,例如given().log().all()代表请求,then().log().all()代表回复

答案 1 :(得分:0)

public class Request {

    public static void main(String[] args) {

    RestAssured.baseURI="http://dummy.restapiexample.com";
    given().    
    //queryParam("key","AIzaSyDIQgAh0B4p0SdyYkyW8tlG-y0yJMfss5Y").
    body("{\"name\":\"test111\",\"salary\":\"123\",\"age\":\"23\"}").
    when().
    post("/api/v1/create").
    then().assertThat().statusCode(200).and().contentType(ContentType.JSON).and()
    .body("status",equalTo("success")).log().body();
    System.out.println("Done");
    }

}

结果:

{
    "status": "success",
    "data": {
        "name": "test111",
        "salary": "123",
        "age": "23",
        "id": 65
    }
}
Done
相关问题