首页 > 解决方案 > 通过场景大纲断言空值

问题描述

我正在尝试通过下面的功能文件断言 JSON 响应空值

Scenario Outline: GET incident notifications
    Given I made a GET request for incident notifications for the "<incident>"
    Then I should be able to see "<NotificationID>", "<DateTime>", "<ActionID>", "<Subject>", "<CreatedBy>","<Notes>"
    Examples:
      | incident |NotificationID|DateTime              |ActionID|Subject              |CreatedBy|Notes|
      | 399      | 211          |2017-11-28T14:30:11.01|0       |Logged with Openreach|         |     |
      | 400      | 2112         |2017-11-28T14:35:11.01|1       |Processed at Openreach|Agent   | AgentNotes    |

这是我的步骤定义-

assertThat(webModel.getRestServices().response.getBody().path("CreatedBy[0]"),is(CreatedBy));
        assertThat(webModel.getRestServices().response.getBody().path("Notes[0]"),is(Notes));

这是我得到的错误断言错误-

java.lang.AssertionError:
Expected: is ""
     but: was null

我可以通过断言来让这个东西工作,nullValue()但是第二次运行将失败,因为它必须从功能文件中获取参数。任何帮助将不胜感激。

标签: cucumberrest-assured

解决方案


当我们在特征文件中将任何参数设为空白时,将其视为空。这就是您收到此错误的原因,因为它正在将 null 与字符串进行比较。我们可以通过在断言之前进行空或空检查来解决这个问题。如果值为 null,它将跳过断言,否则将断言。


推荐阅读