Wednesday 21 November 2018

SAP ABAP Unit test class with Test double

Introduction


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:


1. Interface Injection

◈ Setter method
◈ Constructor method

2. Test Double
3. Test seam
4. Environment injection


Approach



For creating test double, ABAP test double framework is a standardized solution. Test double will be created for the global interface/instance.

SAP Testing and Analysis, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certification

Prerequisites


◈ Test double can be implemented on global interface.

Creation of ABAP unit test class:


1. Go to SE80 and Right click on that class which you want to create a local test class.
2. Click on Create.
3. Click on Generate Test Class.
4. Click on Continue.
5. Select Global Class radio button and Click on
6. Enter the Test class name and select all the check boxes.
7. Click on
8. Select the methods which you want to create test method and Click on
9. Select all the methods and Click on
10. Click on

Steps for creating test double:


1. 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.

2. 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.

3. 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

◈ RAISE_EVENT
◈ IGNORE_PARAMETER
◈ IGNORE_ALL_PARAMETERS
◈ TIMES
◈ AND_EXPECT
◈ SET_ANSWER
◈ SET_MATCHER

4. Specify which method you need to stub.

Example:

◈ 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’

                           );  

Limits:

1. Test double framework is only applicable on interfaces not on classes, local instances and static methods.
2. Stubbing data is not possible for CHANGING command.

No comments:

Post a Comment