Show TOC

Function documentationComplex Comparisons of ABAP Objects Locate this document in the navigation structure

 

You can use a special capability of the CL_ABAP_UNIT_ASSERT=>ASSERT_EQUALS method to perform complex comparisons between expected and actual object references. By default, ASSERT_EQUALS tests object references to point to the same object.

Perform complex object comparisons by doing the following:

  1. Implementing a test verification class that implements the interface IF_AUNIT_OBJECT.

    Your special logic for comparing a referenced object with its test counterpart must be implemented in the EQUALS_TO method of IF_AUNIT_OBJECT.

  2. Passing an instance of your test verification class to CL_ABAP_UNIT_ASSERT->ASSERT_EQUALS as the argument of the EXP parameter.

    EXP contains the data object that is expected to result from the unit test. The EXP object is the standard against which the object produced by the unit test is tested.

Example

Assume that a method to be tested creates an instance of a global class. After calling the MAIN method, then one of the following must be true:

  • If VEHICLE_TYPE of the instance of CL_TEST_CLASS has the value TYPE_1, then NR_WHEELS should have the constant value C_HAS_TWO_WHEELS.

  • If VEHICLE_TYPE of the instance of CL_TEST_CLASS has the value TYPE_2, then NR_WHEELS should either the constant value C_HAS_3_WHEELS or C_HAS_4_WHEELS.

You could verify that these conditions are met in a unit test with CL_ABAP_UNIT_ASSERT->ASSERT_EQUALS and an implementation of IF_AUNIT_OBJECT.

Here is the MAIN method that is to be tested. It sets the vehicle type and number of wheels, the latter based on a differentiator property.

Syntax Syntax

  1. METHOD MAIN.
    
      me->vehicle_type = vehicle_type.
    
    * Set number of wheels.
      CASE vehicle_type.
        WHEN type_1.
          CASE differentiator.
            WHEN class_1.
              me->nr_wheels = c_has_two_wheels.
            WHEN OTHERS.
              me->nr_wheels = c_unknown.
          ENDCASE.
        WHEN type_2.
          CASE differentiator.
            WHEN class_9 or class_10.
              me->nr_wheels = c_has_three_wheels.
            WHEN class_4 or class_5.
              me->nr_wheels = c_has_four_wheels.
            WHEN OTHERS.
              me->nr_wheels = c_unknown.
           ENDCASE.
         WHEN OTHERS.
      ENDCASE.
    
    ENDMETHOD.
End of the code.

Here is the code for the EQUALS_TO method in a class that implements IF_AUNIT_OBJECT. This method is used to verify the referenced object in a unit test of the MAIN method above.

Syntax Syntax

  1. METHOD if_aunit_object~equals_to.
    
    * Test whether the object reference created in the unit test
    * meets one of these conditions:
    * - If VEHICLE_TYPE of the instance of CL_TEST_CLASS has the value TYPE_1,
    *   then NR_WHEELS should have the constant value C_HAS_TWO_WHEELS..
    * - If VEHICLE_TYPE of the instance of CL_TEST_CLASS has the value TYPE_2,
    *   then NR_WHEELS should either the constant value HAS_3_WHEELS or HAS_4_WHEELS. 
    * ...
    
      CONSTANTS: type_1 TYPE c VALUE '1',
                 type_2 TYPE c VALUE '2',
                 c_has_two_wheels TYPE c VALUE '2',
                 c_has_three_wheels TYPE c VALUE '3',
                 c_has_four_wheels TYPE c VALUE '4'.
    
    * Stand-in for a database table of permissible types
      TYPES: BEGIN OF t_types,
        vehicle_type TYPE c LENGTH 1,
        nr_wheels TYPE c LENGTH 1,
       END OF t_types.
    
      DATA vehicle TYPE REF TO cl_implement_if_aunit_object.
    
      DATA t_valid_types TYPE STANDARD TABLE OF t_types
        WITH DEFAULT KEY.
      DATA ls_valid_type TYPE t_types.
    
    * Test data
      ls_valid_type-vehicle_type = type_1.
      ls_valid_type-nr_wheels = c_has_two_wheels.
      APPEND ls_valid_type TO t_valid_types.
    
      ls_valid_type-vehicle_type = type_2.
      ls_valid_type-nr_wheels = c_has_three_wheels.
      APPEND ls_valid_type TO t_valid_types.
    
      ls_valid_type-vehicle_type = type_2.
      ls_valid_type-nr_wheels = c_has_four_wheels.
      APPEND ls_valid_type TO t_valid_types.
    
      vehicle ?= other.
    
    * Check whether the referenced object from the unit test
    * has a permissible type and number of wheels combination
      result = abap_false.
    
      LOOP AT t_valid_types INTO ls_valid_type.
        IF vehicle->vehicle_type = ls_valid_type-vehicle_type.
          IF vehicle->nr_wheels = ls_valid_type-nr_wheels.
            result = abap_true.
            EXIT.
          ENDIF.
        ENDIF.
      ENDLOOP.
    
    ENDMETHOD.
End of the code.

Finally, here is the code for a unit test method that uses IF_AUNIT_OBJECT to verify the MAIN method. The first call to MAIN in the test method passes the unit test. The second call to MAIN results in a unit test error.

Syntax Syntax

  1. CLASS lcl_Unit_Test IMPLEMENTATION.
    * ===================================
    
      METHOD main.
    * ============
        DATA vehicle_Type TYPE c LENGTH 1.
        DATA differentiator TYPE c LENGTH 1.
        DATA assertion_failed TYPE abap_bool.
        DATA lcl_aunit_object TYPE REF TO 
              cl_sp_test_aunit_object.
    
        CREATE OBJECT f_Cut.
    
        f_Cut->main(
            VEHICLE_TYPE = '1'
            DIFFERENTIATOR = 'A' ).
    
        CREATE OBJECT lcl_aunit_object.
    
        cl_abap_unit_assert=>assert_equals(
          EXPORTING
            exp                  = lcl_aunit_object
            act                  = f_Cut ).
    
        f_Cut->main(
            VEHICLE_TYPE = '2'
            DIFFERENTIATOR = 'F' ).
    
        cl_abap_unit_assert=>assert_equals(
          EXPORTING
            exp                  = lcl_aunit_object
            act                  = f_Cut ).
    
      ENDMETHOD.       "main
    
End of the code.