Skip to Content

How to double a methode in Unit-Test

Hello,

i want to test a Methode that calls a provate Methode of the same class. This provate Methode i want to double, using the double Framework.

The Problem i face is, how to tell SAP to use the doubled Methode?

Can anybod help me?

What i got so far is:

DATA: lo_double TYPE REF TO z_cl_double.

lo_double ?= cl_abap_testdouble=>create( 'Z_IF_ADAPTER' ).

cl_abap_testdouble=>configure_call( lo_double )->returning( '10' )->ignore_all_parameters( ).
lo_double->b( ).

But how to tell my class to use the doubled Methode?

Thanks a lot,

Markus

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Mar 29 at 06:51 PM

    Here is a little example. Does this help?

    CLASS tcl_testdouble DEFINITION FOR TESTING
    RISK LEVEL HARMLESS
    DURATION SHORT.
    
      PRIVATE SECTION.
        METHODS test_data_call FOR TESTING.
    ENDCLASS.
    
    CLASS tcl_testdouble IMPLEMENTATION.
      METHOD test_data_call.
        DATA lt_t000_act TYPE STANDARD TABLE OF t000.
        DATA lt_t000_exp TYPE STANDARD TABLE OF t000.
    
        lt_t000_act = VALUE #( ( mandt = '000' )
                               ( mandt = '001' ) ).
    
        DATA lo_testdouble_data TYPE REF TO zjkif_testdouble_data.
    
        lo_testdouble_data = CAST #( cl_abap_testdouble=>create(
                                object_name = 'zjkif_testdouble_data'
    *                            double_name =
                            ) ).
    
        cl_abap_testdouble=>configure_call( double = lo_testdouble_data )->returning( value = lt_t000_act  ).
        lo_testdouble_data->get_t000_data( ).
    
        DATA(lo_cut) = NEW zjkcl_testdouble( lo_testdouble_data ).
    
        DATA(lt_t000) = lo_cut->get_t000_data( ).
    
        lt_t000_exp = VALUE #( ( mandt = '000' )
                               ( mandt = '001' ) ).
    
        cl_abap_unit_assert=>assert_equals(
          EXPORTING
            act                  = lt_t000    " Data object with current value
            exp                  = lt_t000_exp    " Data object with expected type
        ).
    
      ENDMETHOD.
    
    ENDCLASS.
    
    Add comment
    10|10000 characters needed characters exceeded