REST Assured 系列汇总 之 REST Assured 13 - API Response写入JSON文件
需求:
我们可能需要存储API Response到JSON文件,作为测试结果供后面参考。或则需要将JSON Response存储起来作为另外一些API的输入。我们也可以存储JSON Response供以后提取其中的有用的信息。
需要的依赖包:
需要Rest Assured 和Junit依赖包。Junit包,我们需要用“Files”类来自Google Common 依赖包。而Junit依赖有传递依赖Guava。所以Junit可以用作测试框架和文件处理工具。当然TestNG跟Jnuite一样效果。
<!-- REST Assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.7</version>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
Rest Assured提供下面方法获取Response:
As byte array :- asByteArray()
As input stream :- asInputStream()
As string :- asString()
上面这些方法的输出可以直接写入JSON文件。
Code:
import com.google.common.io.Files;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class WriteResponseInTextFile {
@Test
public void writeResponse() throws IOException {
// There is no need to add escape character manually. Just paste string within double
// quotes. It will automatically add escape sequence as required.
String jsonString = "{\"username\" : \"admin\",\"password\" : \"password123\"}";
RequestSpecification request = RestAssured.given();
//Adding URI
request = request.baseUri("https://restful-booker.herokuapp.com/auth");
// Setting content type to specify format in which request payload will be sent.
// ContentType is an ENUM.
request = request.contentType(ContentType.JSON);
// Adding body as string
request = request.body(jsonString);
// Calling POST method on URI. After hitting we get Response
Response response = request.post();
// Printing Response as string
System.out.println(response.asString());
// Getting response as a string and writing in to a file
String responseAsString = response.asString();
// Converting in to byte array before writing
byte[] responseAsStringByte = responseAsString.getBytes();
// Creating a target file
File targetFileForString = new File("src/main/resources/targetFileForString.json");
// Writing into files
Files.write(responseAsStringByte, targetFileForString);
// Getting response as input stream and writing in to a file
InputStream responseAsInputStream = response.asInputStream();
// Creating a byte array with number of bytes of input stream (available()
// method)
byte[] responseAsInputStreamByte = new byte[responseAsInputStream.available()];
// Reads number of bytes from the input stream and stores them into the byte
// array responseAsInputStreamByte.
responseAsInputStream.read(responseAsInputStreamByte);
// Creating a target file
File targetFileForInputStream = new File("src/main/resources/targetFileForInputStream.json");
// Writing into files
Files.write(responseAsInputStreamByte, targetFileForInputStream);
// Directly getting a byte array
byte[] responseAsByteArray = response.asByteArray();
// Creating a target file
File targetFileForByteArray = new File("src/main/resources/targetFileForByteArray.json");
// Writing into files
Files.write(responseAsByteArray, targetFileForByteArray);
}
}
输出:
三个文件生成: