测试场景创建 + 验证

ABAP RESTful 应用程序编程模型 › 测试 › 集成测试 › EML 集成测试 › EML 测试:实施测试方法 › 测试场景创建 + 验证

此集成测试检查验证 validateDates 是否阻止包含无效航班数据的已创建差旅实例的持久性。

测试方法 create_validation_negative

此测试方法测试使用者尝试使用无效航班日期创建差旅实例的情况。因此,在给定示例中,将过去的开始日期分配给变量 begin_date。变量 end_date 获得有效的航班日期。

现在, 方法使用准备的变量对业务对象执行 EML 修改创建调用。由于该方法仅关注验证 validateDates,因此提供有效的代理和客户数据。

创建操作应正确运行,因为验证 validateDates 仅作为保存序列的一部分执行,将在后续 EML 语句中触发。因此,该方法断言来自 EML 创建语句的 mapped 响应包含预期内容和差旅标识,并且 failed 和 reported 响应为空。

现在,测试方法使用 COMMIT ENTITIES 语句触发执行验证 validateDates 的保存顺序。

由于提供的开始日期在过去,因此该方法断言 failed 操作的 COMMIT ENTITIES 响应包含失败实例的行程标识。此外,该方法断言系统字段 sy-subrc 包含返回值 4,表示 check_before_save 未成功执行。

根据验证实施的逻辑,如果提供了错误的开始日期,则在 reported 响应中同时标记开始日期和结束日期字段。测试方法指定相应的 assert 语句对此进行检查。此外,方法 断言正确的消息信息包含在 reported 响应中。

展开以下代码示例以查看方法 create_validation_negative 的源代码。

示例代码

METHOD create_validation_negative.

    begin_date = cl_abap_context_info=>get_system_date( ) - 10.
    end_date   = cl_abap_context_info=>get_system_date( ) + 30.

    " Create a Travel with invalid begin_date
    MODIFY ENTITIES OF /dmo/i_travel_m
      ENTITY travel
        CREATE FIELDS ( agency_id customer_id begin_date end_date description booking_fee currency_code overall_status ) WITH
        VALUE #( (         %cid = cid
                           agency_id      = agency_mock_data[ 1 ]-agency_id
                           customer_id    = customer_mock_data[ 1 ]-customer_id
                           begin_date     = begin_date
                           end_date       = end_date
                           description    = 'TestTravel 1'
                           booking_fee    = '10.5'
                           currency_code  = 'EUR'
                           overall_status = 'O' ) )
     MAPPED DATA(mapped_create)
     FAILED DATA(failed_create)
     REPORTED DATA(reported_create).

    " Travel was created successfully
    cl_abap_unit_assert=>assert_equals( exp = 1 act = lines( mapped_create-travel ) ).
    DATA(key_of_new_travel) = mapped_create-travel[ 1 ].
    cl_abap_unit_assert=>assert_equals( exp = cid act = key_of_new_travel-%cid ).
    cl_abap_unit_assert=>assert_not_initial( act = key_of_new_travel-travel_id ).

    cl_abap_unit_assert=>assert_initial( act = failed_create ).
    cl_abap_unit_assert=>assert_initial( act = reported_create ).

    " Trigger Validations
    COMMIT ENTITIES
    RESPONSE OF /DMO/I_Travel_M
    FAILED DATA(failed_commit)
    REPORTED DATA(reported_commit).

    " Commit failed as begin_date is invalid ( must be after system date )
    cl_abap_unit_assert=>assert_subrc( exp = 4 ).

    cl_abap_unit_assert=>assert_equals( act = lines( failed_commit-travel ) exp = 1 ).
    cl_abap_unit_assert=>assert_not_initial( act = failed_commit-travel[ 1 ]-travel_id ).

    cl_abap_unit_assert=>assert_equals( act = lines( reported_commit-travel ) exp = 1 ).
    cl_abap_unit_assert=>assert_not_initial( act = reported_commit-travel[ 1 ]-travel_id  ).
    cl_abap_unit_assert=>assert_equals( act = reported_commit-travel[ 1 ]-%element-begin_date  exp = if_abap_behv=>mk-on ).
    cl_abap_unit_assert=>assert_equals( act = reported_commit-travel[ 1 ]-%element-end_date  exp = if_abap_behv=>mk-on ).
    cl_abap_unit_assert=>assert_equals( exp = 004 act = reported_commit-travel[ 1 ]-%msg->if_t100_message~t100key-msgno ).
    cl_abap_unit_assert=>assert_equals( exp = '/DMO/CM_FLIGHT' act = reported_commit-travel[ 1 ]-%msg->if_t100_message~t100key-msgid ).

  ENDMETHOD.

测试方法 create_validation_positive

第二个测试方法测试使用者使用有效航班数据创建差旅实例的情况。与上面显示的负面情况相反,现在预期 failed 和 COMMIT ENTITIES reported 语句的响应为空。系统字段 sy-subrc 应包含值 0,表示提交成功。

展开以下代码示例以查看方法 create_validation_positive 的源代码。

示例代码

METHOD create_validation_positive.

    begin_date = cl_abap_context_info=>get_system_date( ) + 10.
    end_date   = cl_abap_context_info=>get_system_date( ) + 30.

    " Create a Travel with valid begin_date
    MODIFY ENTITIES OF /dmo/i_travel_m
      ENTITY travel
        CREATE FIELDS ( agency_id customer_id begin_date end_date description booking_fee currency_code overall_status ) WITH
        VALUE #( (         %cid = cid
                           agency_id      = agency_mock_data[ 1 ]-agency_id
                           customer_id    = customer_mock_data[ 1 ]-customer_id
                           begin_date     = begin_date
                           end_date       = end_date
                           description    = 'TestTravel 1'
                           booking_fee    = '10.5'
                           currency_code  = 'EUR'
                           overall_status = 'O' ) )
     MAPPED DATA(mapped_create)
     FAILED DATA(failed_create)
     REPORTED DATA(reported_create).

    " Travel was created successfully
    cl_abap_unit_assert=>assert_equals( exp = 1 act = lines( mapped_create-travel ) ).
    DATA(key_of_new_travel) = mapped_create-travel[ 1 ].
    cl_abap_unit_assert=>assert_equals( exp = cid act = key_of_new_travel-%cid ).
    cl_abap_unit_assert=>assert_not_initial( act = key_of_new_travel-travel_id ).

    cl_abap_unit_assert=>assert_initial( act = failed_create ).
    cl_abap_unit_assert=>assert_initial( act = reported_create ).

    " Trigger Validations
    COMMIT ENTITIES
    RESPONSE OF /DMO/I_Travel_M
    FAILED DATA(failed_commit)
    REPORTED DATA(reported_commit).

    " Commit was successful
    cl_abap_unit_assert=>assert_subrc( exp = 0 ).
    cl_abap_unit_assert=>assert_initial( act = failed_commit ).
    cl_abap_unit_assert=>assert_initial( act = reported_commit ).

  ENDMETHOD.

来源:SAP Help Portal · 2025 FPS02 (Oct 2026) · 中文为 SAP 官方机器翻译

← EML 测试:实施测试方法 · 测试场景深度创建 + 确定 →