ABAP Unit is the new unit test tool for ABAP which solves the problems with developer tests. Unit tests are a methodology for software development and are a mature framework in the developer toolbox. Tests can be conveniently grouped into test tasks. Test results are condensed and become evident at once. Test tasks can be performed by automation. In this document we will discuss how to automate Test Double Framework which is used to remove dependency from main CUT (Class under test) class.
Types to remove dependency:
- Interface Injection
- Setter method
- Constructor method
- Test Double
- Test seam
- Environment injection
For creating test double, ABAP test double framework is a standardized solution. Test double will be created for the global interface/instance.
- Test double can be implemented on global interface.
Creation of ABAP unit test class:
- Go to SE80 and Right click on that class which you want to create a local test class.
- Click on Create.
- Click on Generate Test Class.
- Click on Continue.
- Select Global Class radio button and Click on
- Enter the Test class name and select all the check boxes.
- Click on
- Select the methods which you want to create test method and Click on
- Select all the methods and Click on
- Click on
Steps for creating test double:
- Creation of test double object for dependency object.
Method setup. CREATE OBJECT mo_cut. “Creates object of CUT class mo_double ?= cl_abap_testdouble=>create( ‘/ZIF/DEMO’). ENDMETHOD.
- Inject the test double into the object which is being tested.
mo_cut->mo_test =mo_double.“mo_test is the global object of CUT class.
- Configure call and set the desired value for the method for which you are making double.
- SET_PARAMETER: Used when the stub method does not return anything and just has importing parameters.
Example: DATA(lo_double) = cl_abap_testdouble=>configure_call(mo_double). lo_double->set_parameter( name = 'ev_data' value = 001 ). “Follow step4
Note: Create SET_PARAMETER per each exporting parameter from the stubbed method.
- RAISE_EXCEPTION: Used when you want stubbed method to raise any exception. Create object of type exception that the dependent method has and pass it to configure call.
Example: CREATE OBJECT lo_exp TYPE zcl_exception. DATA(lo_double) = cl_abap_testdouble=>configure_call(mo_double). lo_double->raise_exception(lo_exp). “Follow step4
Note: lo_exp object should be created before the call of RAISE_EXCEPTION.
- RETURNING:Used when stub method returns value.
Example: DATA(lo_double) = cl_abap_testdouble=>configure_call(mo_double). lo_double->returning(lt_data). “Follow step4
Before assigning lt_data parameter, fill all necessary data which you wanted from the stubbed method.
These are few other methods used for configuration call. Sample code for below configuration call has been demonstrated in the following blog: Short examples of CL_ABAP_TESTDOUBLE – Sandra Rossi(https://blogs.sap.com/2018/04/03/short-examples-of-cl_abap_testdouble/)
- Specify which method you need to stub.
- Stubbing for SET_PARAMETER–
Fill all mandatory importing parameter of stubbed method.
mo_cut->mo_test->set_data( iv_client = ‘X’ iv_text = ‘SAMPLE’ );
When SET_DATA method of /ZIF/DEMO is triggered then ‘EV_DATA’ will be filled automatically.
- Stubbing for RAISE_EXCEPTION–
GET_EXCEPTION method is explicitly raising exception for CUT. All mandatory import parameters should be filled before stubbed method.
mo_cut->mo_test->get_exception( iv_text = ‘SAMPLE’ );
- Stubbing for RETURNING–
When GET_DATA method is triggered it will return LT_DATA to the CUT. All mandatory import parameters should be filled before stubbed method.
mo_cut->mo_test->get_data( iv_text = ‘SAMPLE’ );
- Test double framework is only applicable on interfaces not on classes, local instances and static methods.
- Stubbing data is not possible for CHANGING command.
Test double framework is a good approach for creating ABAP unit test classes. This approach can be deployed on Global interface which can mock dependent interface methods. The unit test class will help the developers to create automation testing.