CL_CRM_UNIT_CONSTS - Provides system-dependent constants for ABAPUnit Tests

ABAP Short Reference   ROGBILLS - Synchronize billing plans  
This documentation is copyright by SAP AG.

Functionality

Unit tests are supposed to run in different systems, at least the development system and the unit testsystem. Unit tests almost always rely on system-dependent data. It most of the cases it is impossible or too much of an effort to write unit tests that create the complete data environment needed for the execution.

Let's take as an example a unit test that creates an order for a certain business partner. The businesspartner id is system-dependent. If you would put the business partner id as a constant into your code,the test would only work in the development system. It would be unlikely that a suitable business partner with the same id would exist in the test system. The unit test then would fail in the test system because of this.

One solution is that you store these kind of test constants in a database table that you maintain with different values in development and test system.

The database table CRM_UNIT_CONSTS was created for this purpose and this class to access this database from your unit test.

The table CRM_UNIT_CONSTS has the fields

  • CLASSNAME (key) - Name of the class or function group the unit test belongs to. PLEASE ADD THE LEADING "SAPL" FOR FUNCTION GROUPS TO AVOID NAMING CONFLICTS BETWEEN CLASSES AND FUNCTION GROUPS.
  • CONST_KEY (key) - Name of the test constant.
  • CONST_VALUE - Value of the test constant.

The table can be maintained via transaction SM31.

To use this class in your unit test you have to

  • Instantiate the class CL_CRM_UNIT_CONSTS with the name of the class your unit test belongs to. (You normally do this in the method CLASS_SETUP that it called during initialization of your test class.)
  • Call method GET_CONST_ASSERT to read a particular constant.

The method GET_CONST_ASSERT throws a test failure and stops execution of the current test method, ifthe given constant key could not be found. It writes a message to the unit test protocol which constant could not be read.

Example

Here a code sample for an ABAPUnit test to test a method cl_my_order_class=>create_order.

The table CRM_UNIT_CONSTS stores the business partner id used as sold-to for the order creation. The entry looks as follows:

  • CLASSNAME = CL_MY_ORDER_CLASS
  • CONST_KEY = BP_ID
  • CONST_VALUE = 4711.

Here the sample for the local test class included in the class CL_MY_ORDER_CLASS.

CLASS abap_unit_testclass DEFINITION FOR TESTING
  "#AU Duration Medium
  "#AU Risk_Level Harmless

* --------------------------------------------------------------
* ================
  PRIVATE SECTION.
* ================
    CLASS-DATA:
      unit_consts TYPE REF TO cl_crm_unit_consts.

    CLASS-METHODS: class_setup.
    METHODS: order_create FOR TESTING.

ENDCLASS.       "Abap_Unit_Testclass

* --------------------------------------------------------------
CLASS abap_unit_testclass IMPLEMENTATION.
* --------------------------------------------------------------

* --------------------------------------------------------------
  METHOD class_setup.
* --------------------------------------------------------------
    CREATE OBJECT unit_consts
      EXPORTING
        class_name = 'CL_MY_ORDER_CLASS'.
  ENDMETHOD.                    "class_setup

* --------------------------------------------------------------
  METHOD order_create.
* --------------------------------------------------------------
    DATA:
      bp_id TYPE BU_PARTNER,
      order_id TYPE i.

    bp_id = unit_consts->get_const_assert( const_key = 'SOLD_TO_ID' ).

    order_id = cl_my_order_class=>create_order( sold_to = bp_id ).

    cl_aunit_assert=>assert_not_initial(
      act   = order_id
      msg   = 'No order id returned').
  ENDMETHOD.                   "class_setup

ENDCLASS.       "Abap_Unit_Testclass



Fill RESBD Structure from EBP Component Structure   Fill RESBD Structure from EBP Component Structure  
This documentation is copyright by SAP AG.


Length: 5291 Date: 20120526 Time: 132351     triton ( 176 ms )