More of an announcement rather than a blog: Drumroll for the latest in Unit Testing Frameworks, Function Modules.

Function modules have always been the thorn in the side of anyone trying to write ABAP Unit Tests. The only sensible way to mock a call to a function module was to wrap the FM into a class method and then mock that, either via the OO Test Double Framework or by coding explicit test doubles.

Well, the next shiny new version of ABAP (7.56) includes a Function Module test double. And while I could write a nice blog with examples, SAP have stolen my thunder and done a sterling job of documenting it in good detail with great examples over here. Personally I really like the simple, easy to understand way the framework is structured.

For those on 7.56, enjoy! The rest of us will have to be very patient. But at least for those on lower versions it’s always handy to know what’s coming in order to avoid spending too much resources on reinventing a future wheel.

Have Fun!

 

Update: Following a great hint from Thomas Fiedler that this is available in the ABAP Cloud trial system, I managed to give it a little go. I used a simple method that calls BAPI_MESSAGE_GETDETAIL and returns the message text.

  method get_message.

    data: message type bapiret2-message,
          return  type bapiret2.

    call function 'BAPI_MESSAGE_GETDETAIL'
      exporting
        id         = '00'
        number     = 000
        textformat = 'HTM'
      importing
        message    = message
        return     = return.

    result = message.

  endmethod.

A quirk I discovered was that, mocking a call where I don’t care about the input to the FM proved to be more complex than setting up a scenario with expected input parameters. One needs to implement interface if_ftd_invocation_answer instead of just calling create_input_configuration.

For convenience and brevity I implemented the interface in the test class so I could control everything in one class and pass me as the answering object, thus:

class ltc_test definition final for testing
  duration short
  risk level harmless.

  public section.
    interfaces if_ftd_invocation_answer.

  private section.
    constants mock_text type string value `The moon is made of green cheese`.

    methods fm_is_mocked for testing raising cx_static_check.

endclass.


class ltc_test implementation.

  method fm_is_mocked.

    data(function_double) = cl_function_test_environment=>create(
      value #( ( 'BAPI_MESSAGE_GETDETAIL' ) ) )->get_double( 'BAPI_MESSAGE_GETDETAIL' ).

    function_double->configure_call( )->ignore_all_parameters( )->then_answer( me ).

    cl_abap_unit_assert=>assert_equals( act = new zcl_message( )->get_message( )
                                        exp = mock_text ).

  endmethod.

  method if_ftd_invocation_answer~answer.
    result->get_output_configuration( )->set_exporting_parameter( name  = 'MESSAGE' 
                                                                  value = mock_text ).
  endmethod.

endclass.

 

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x