当前位置: 首页 > 知识库问答 >
问题:

Hamcrest.Matchers: JSON Nested Array

阴英武
2023-03-14

我有一个数组如下:[[禁用,启用,否,是][乔,比尔,道格,凯文],[红,蓝,粉红,洋红],[一,二,三,四];

我需要一个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进行断言。

共有1个答案

昝浩阔
2023-03-14

AFAIK Hamcrest中不包含Excel集成。simple excel提供对生成的excel文件的验证,并为此带来了自己的Hamcrest匹配器。

否则,您很可能不得不使用ApachePOI或Jetcel来进行自己的匹配。

 类似资料:

相关问答

相关文章

相关阅读