[[{“value”:”
Introduction
Unit testing in ABAP has branch wise and statement wise testing in modern SAP development.
While SAP provides powerful features for testing classes and interfaces, even covering branches is important in Function Modules (FM).
Function Modules are difficult to test because:
- They are procedural, not object-oriented
- They have no native test-double support like classes
- They perform external operations (DB calls, RFC, BAPIs, etc.)
To solve this, SAP introduced the Function Test Double Framework, making it possible to simulate FM behavior during ABAP Unit tests.
In this blog, I will demonstrate how to:
- Create a test class
- Use CL_FUNCTION_TEST_ENVIRONMENT
- Create input/output configurations
- Simulate a Function Module call (function double)
- Test your application class without calling the real FM
Step-by-Step Explanation
Step 1: Create Test Class with Setup / Teardown
We define a local test class implementing the ABAP Unit framework:
PRIVATE SECTION.
CLASS-DATA : gv_func_env TYPE REF TO if_function_test_environment,
lo_ref TYPE REF TO zcl_jag_new.
CLASS-METHODS :
class_setup,
class_teardown.
METHODS :
add_new FOR TESTING.
Step 2: Initialize Function Test Environment
In class_setup, we register the function module to be doubled:
METHOD class_setup.
gv_func_env = cl_function_test_environment=>create(
VALUE #( ( ‘ZFM_JAG_ADD’ ) ) ).
ENDMETHOD.
This tells ABAP Unit:
“Whenever this test calls ZFM_JAG_ADD, do NOT call the real FM.
Use a test double instead.”
Step 3: Clear Test Doubles
In teardown, we clean the test environment:
METHOD class_teardown.
gv_func_env->clear_doubles( ).
ENDMETHOD.
Step 4: Create Input Configuration for the Double
The double should respond only when specific parameters are passed.
DATA(lo_test_double) = gv_func_env->get_double( ‘ZFM_JAG_ADD’ ).
DATA(lo_input) = lo_test_double->create_input_configuration( )->set_importing_parameter(
name = ‘NUM1’
value = ‘1’
).
This tells the double:
When NUM1 = 1 → match this configuration
Step 5: Create Output Configuration for the Double
Define what the double should return:
DATA(lo_output) = lo_test_double->create_output_configuration( )->set_exporting_parameter(
name = ‘RESULT’
value = ‘1’
).
Meaning:
When NUM1 = 1 → return RESULT = 1
Step 6: Bind Input + Output via configure_call()
lo_test_double->configure_call( )
->when( lo_input )
->then_set_output( lo_output ).
This completes the test double behavior.
Step 7: Call the Application Class (not the FM)
lo_ref = NEW zcl_jag_new( ).
lo_ref->add_new(
EXPORTING
num1 = 1
num2 = 2
IMPORTING
result = result
).
Step 8: Assert the Result
cl_abap_unit_assert=>assert_equals(
act = result
exp = result
).
- Full Source Code (Ready for SAP Blog)
CLASS zlcl_jag_add DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
PRIVATE SECTION.
CLASS-DATA : gv_func_env TYPE REF TO if_function_test_environment,
lo_ref TYPE REF TO zcl_jag_new.
CLASS-METHODS :
class_setup,
class_teardown.
METHODS :
add_new FOR TESTING.
ENDCLASS.
CLASS zlcl_jag_add IMPLEMENTATION.
METHOD class_setup.
gv_func_env = cl_function_test_environment=>create(
VALUE #( ( ‘ZFM_JAG_ADD’ ) ) ).
ENDMETHOD.
METHOD class_teardown.
gv_func_env->clear_doubles( ).
ENDMETHOD.
METHOD add_new.
DATA result TYPE int1.
DATA(lo_test_double) = gv_func_env->get_double( ‘ZFM_JAG_ADD’ ).
DATA(lo_input) = lo_test_double->create_input_configuration( )->set_importing_parameter(
name = ‘NUM1’
value = ‘1’
).
DATA(lo_output) = lo_test_double->create_output_configuration( )->set_exporting_parameter(
name = ‘RESULT’
value = ‘1’
).
lo_test_double->configure_call( )
->when( lo_input )
->then_set_output( lo_output ).
lo_ref = NEW zcl_jag_new( ).
lo_ref->add_new(
EXPORTING
num1 = 1
num2 = 2
IMPORTING
result = result
).
cl_abap_unit_assert=>assert_equals(
act = result
exp = result
).
ENDMETHOD.
Conclusion: The above code helps to set expected exporting parameters for a function module as user expected and same set as exporting parameters will apply through out the class where ever same function module called multiple times.
“}]]
Read More Technology Blog Posts by Members articles
#abap