I need to test the API that uses the JSON format, so I will summarize it below. I'm verifying it, so I think there are some mistakes.
rest-assured will automatically convert the Object class used for the request to Json. This time I'm using Jackson 2.
--The existence of the classpath is confirmed and serialization is performed in the following order.
Since ObjectMapper is used, specify its settings and connection destination.
//api connection destination host name
RestAssured.baseURI = "http://localhost"
//ObjectMapper settings
RestAssured.config = RestAssuredConfig.config().objectMapperConfig(
ObjectMapperConfig.objectMapperConfig().jackson2ObjectMapperFactory(new Jackson2ObjectMapperFactory() {
@SuppressWarnings("rawtypes")
@Override
public ObjectMapper create(Class cls, String charset) {
ObjectMapper objectMapper = new ObjectMapper();
//ObjectMapper settings here
return objectMapper;
}
})
);
//static import
import static io.restassured.RestAssured.given;
ResponseClass response = given()
.contentType(ContentType.JSON) // content type
.body(reqest) //request
.when()
.post("/api/hoge/1") //Specify API endpoint
.then()
.statusCode(200)
.extract().as(ResponseClass.class); //Deserialize to specified class
Use junit assert to see if the expected value is returned.
Assert.assertThat(response.hoge, is("hoge"));
If you use DbUnit, you can compare the current DB record and the data written to Excel as the expected value. It is also convenient because you can write DB records to an Excel file.
Connection connection = DriverManager.getConnection(url, username, password)
IDatabaseConnection iDbConnection = new IDatabaseConnection(connection);
//Data after execution
IDataSet dataSet = dbConn.createDataSet();
//Get the record with the specified table name
ITable data= dataSet.getTable("table");
//Set columns you want to exclude from assert
data = DefaultColumnFilter.excludedColumnsTable(iTable, "created_at");
//Sort by column
data= new SortedTable(iTable, "hoge_name");
//Expected value data(Excel)
ITable Expected Value= new XlsDataSet(new File("hoge/huga/Expected value.xls"));
Expected value= DefaultColumnFilter.excludedColumnsTable(iTable, "created_at");
Expected value= new SortedTable(iTable, "hoge_name");
//DbUnit Assertion, not junit assertEquals#Be careful to use assertEquals
Assertion.assertEquals(Expected value, data);
--After converting the request Object to Map using ObjectMapper, specify the json key and put any parameter to map.
//Convert request object to MAP
RequestObj request = new RequestObj():
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(request);
LinkedHashMap<String, Object> map = objectMapper.readValue(jsonString, new TypeReference<LinkedHashMap<String, Object>>(){});
//Any key:set value
map.put("dinner", "Grilled meat")
//Convert map to String
String jsonString = objectMapper.writeValueAsString(map);
//Set json string to body and send request
ResponseClass response = given()
.contentType(ContentType.JSON)
.body(jsonString)
.log().body()
.when()
.post(endpoint)
.then()
.log().body()
.statusCode(200)
.extract().as(responseClass);
Recommended Posts