Skip to Content

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
  1. Test Double
  2. Test seam
  3. 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.

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.

 

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

 

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

  • RAISE_EVENT
  • IGNORE_PARAMETER
  • IGNORE_ALL_PARAMETERS
  • TIMES
  • AND_EXPECT
  • SET_ANSWER
  • SET_MATCHER
  1. 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.

Conclusion:

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.

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. Jelena Perfiljeva

    This subject has already been covered in several SCN blogs. Google -> “ABAP test double site: blogs.sap.com”, these are just from the top of the list:

    https://blogs.sap.com/2015/01/05/abap-test-double-framework-an-introduction/

    https://blogs.sap.com/2018/05/08/exploring-the-abap-test-double-framework/

    https://blogs.sap.com/2016/10/19/introduction-cds-test-double-framework-write-unit-tests-abap-cds-entities/

    https://blogs.sap.com/2018/04/03/short-examples-of-cl_abap_testdouble/

    The last blog seems to be referenced in yours, please make sure to include a link when you do that.

    (0) 

Leave a Reply