ABAP单元测试之TEST DOUBLE

TEST DOUBLE是一种面向单元测试的编码方式,使用interface独立相关的第三方服务,保证自身单元功能的独立性。

其原理图如下:

ABAP单元测试之TEST DOUBLE_第1张图片

示例代码 - 待测的全局类zcl_abc_fun01:

CLASS zcl_abc_fun01 DEFINITION
                    PUBLIC
                    FINAL
                    CREATE PUBLIC .
  PUBLIC SECTION.
    METHODS constructor .
    METHODS output_own_logsys
      EXPORTING
        !ev_logsys TYPE logsys .
  PROTECTED SECTION.
  PRIVATE SECTION.
    DATA mo_srv TYPE REF TO lif_srv .
ENDCLASS.

CLASS ZCL_ABC_FUN01 IMPLEMENTATION.

  METHOD constructor.
    mo_srv = NEW lcl_srv( ).
  ENDMETHOD.

  METHOD output_own_logsys.
    ev_logsys = mo_srv->get_own_logsys( ).
  ENDMETHOD.
ENDCLASS.

全局类的local interface & class声明部分:

*"* use this source file for any type of declarations (class
*"* definitions, interfaces or type declarations) you need for
*"* components in the private section

INTERFACE: lif_srv.
  METHODS:
    get_own_logsys
      RETURNING VALUE(rv_logsys) TYPE logsys.
ENDINTERFACE.

CLASS lcl_srv DEFINITION.
  PUBLIC SECTION.
    INTERFACES: lif_srv.
ENDCLASS.

全局类的local interface & class实现部分:

*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations
CLASS lcl_srv IMPLEMENTATION.

  METHOD: lif_srv~get_own_logsys.
    CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'
      IMPORTING
        own_logical_system             = rv_logsys.
  ENDMETHOD.
ENDCLASS.

全局类的Unit Test Class代码:

**********************************************************************
*    Define test-class as a local friend                             *
**********************************************************************
CLASS ltc_abc_fun01 DEFINITION DEFERRED.
CLASS zcl_abc_fun01 DEFINITION LOCAL FRIENDS ltc_abc_fun01.

**********************************************************************
*    Test Double class for external Service                          *
**********************************************************************
CLASS ltd_srv DEFINITION.
  PUBLIC SECTION.
    INTERFACES: lif_srv.
ENDCLASS.
CLASS ltd_srv IMPLEMENTATION.
  METHOD lif_srv~get_own_logsys.
    rv_logsys = 'TEST_500'.
  ENDMETHOD.
ENDCLASS.

**********************************************************************
*    Unit-test class                                                 *
**********************************************************************
CLASS ltc_abc_fun01 DEFINITION FOR TESTING
                    DURATION SHORT
                    RISK LEVEL HARMLESS.
  PRIVATE SECTION.
    DATA:
      f_cut TYPE REF TO zcl_abc_fun01.  "class under test

    METHODS: output_own_logsys FOR TESTING.
ENDCLASS.       "ltc_Abc_Fun01


CLASS ltc_abc_fun01 IMPLEMENTATION.


  METHOD output_own_logsys.

    CREATE OBJECT f_cut.

    f_cut->mo_srv = NEW ltd_srv( ). " insert test double for service

    f_cut->output_own_logsys( IMPORTING ev_logsys = DATA(lv_logsys)  ).

    cl_abap_unit_assert=>assert_equals( act = lv_logsys exp =  'TEST_500').

  ENDMETHOD.
ENDCLASS.

你可能感兴趣的:(ABAP,UT)