我有一个数组如下: [[Disable,Enable,No,Yes][Joe,Bill,Doug,Kevin],[Red,Blue,Pink,Magenta],[One,Two,Three,Four]];
我需要一个 hamcrest 匹配器,该匹配器将与其中包含名称/值对数据的 excel 文件匹配。我尝试过:hasItem、hasItems、hasItemInArray。
我查看了以下链接:
如果我遗漏任何有用的信息,请告诉我。
private String[][] getExcelSheetData(String excelFile)
throws NoExcelValidationDataFoundException {
String[][] excelValidationData = null;
try {
ExcelDAOFactory excelDAOFactory = (ExcelDAOFactory) DAOFactory
.getFactory(DAOFactory.EXCEL);
excelDAOFactory.setExcelFile(excelFile);
excelValidationData = excelDAOFactory.fetchData();
TAFLogger.logTAFActivity("Finished retrieving data from "
+ excelFile);
} catch (InvalidFormatException | IOException | UnsupportedDAOException e) {
TAFLogger.logTAFActivity(
"Error retrieving data from ExcelDAOFactory: "
+ e.getMessage(), logger.WARN);
throw new NoExcelValidationDataFoundException(
"Could not retireve validation " + "data from " + excelFile
+ e.getMessage());
}
return excelValidationData;
}
尝试与 hamcrest 匹配的代码,我可以看到它没有任何“嵌套数组”:
requestBuilder = new RequestSpecBuilder();
requestBuilder.addCookie().setContentType(ContentType.JSON);
requestSpecification = requestBuilder.build();
responseBuilder = new ResponseSpecBuilder();
responseBuilder.expectStatusCode(Integer.parseInt(xmlTest
.getParameter("http-status-code-200")));
//responseBuilder.expectContentType(ContentType.JSON);
responseSpecification = responseBuilder.build();
try {
validationData = getExcelSheetData(excelFile);
} catch (NoExcelValidationDataFoundException e) {
Logger.logActivity("Could not retrieve Excel sheet data: "
+ e.getMessage(), Logger.ERROR);
Assert.fail("Could not retrieve Excel sheet data: "
+ e.getMessage());
for (int i = 0; i < validationData.length; i++) {
responseBuilder.expectBody(validationData[i][0],
hasItemInArray(validationData[i][1]));
responseSpecification = responseBuilder.build();
try {
//Response res = given().getValue()).get("data/roles");
String data = given().spec(requestSpecification).
.expect().spec(responseSpecification).when().get("data/roles").asString();
System.out.println(data);
logger.logTestActivity("Completed testing JSON payload using Excel file");
} catch (AssertionError e) {
logger.logTestActivity(
"Error testing JSON payload: " + e.getMessage(),
logger.ERROR);
throw new Exception(e);
与“hasIteminArray”的最后一个标准是我试图利用 Hamcrest 对嵌套数组的 JSON 进行断言。