Unit Test with SQL Inject/SQL Fake

light111Unit Test with SQL Inject/SQL Fake


Now it is recommended to have unit tests for newly designed methods to test  whether the written business logic in a method behaves properly to different use cases. Most of the time the method contains SQL query  and if unit test is performed on such method then the query will always fail as the test is performed mostly on the dev system/client.

Now in ABAP , here a technique where fake entries can be created in the unit test run time so that the unit test can be  performed for all use cases where the real DB table doesn’t have any record.

The post shows how to make SQL fake in unit test.

Suppose one the cust table looks like as shown below where a Bill Type is derived from Order Type & Item Category.

1

We have the maintenance view( for this unit test we dont need any view) but.. 

2

So here no entries/records present.

3

Below we have a class and method, that reads the cust table and returns the bill type.

45

Here we can have 3 cases:

  • If you don’t provide both entries, then the code does nothing.
  • If you provide both inputs and if it is present in DB, then we receive a bill type
  • If you provide both inputs and if it is not present in DB, then we receive an exception

6

The method can be tested to see the different results:

Test1: Don’t pass any input and the return is blank.

78


If you pass any thing for both the inputs, then we receive an exception as the table doesn’t habve any record.

9


Now lets look into the local test classes.

10

For SQL fake we need a ref to interface – IF_OSQL_TEST_ENVIRONMENT

11

In CREATE method, it is possible to pass multiple table names for which fake test entries will be created for the test purpose. Here we have only one table.

12

we have 3 different test methods for 3 use cases.

131415


Let’s check in debugger. Execute the UNIT test.

16

Here below we pass both the inputs for which no matching record present and here it is expected an exception from the method GET_BILL_TYPE.

17

Here an exception is raised.

18

In the test method, we have the proper assert is checked.

19


Here we don’t pass any inputs and expects that the method GET_BILL_TYPE did not return any value.

2021


Here we pass the two inputs and for the input combination already a fake entry preset in the table. So it returns a valid BILL_TYPE.

22

Here the SY-SUBRC is 0 and we have a bill type and the assert is properly checked.

2324


 

use this source file for your ABAP unit test classes
CLASS ltcl_test_cust_acess DEFINITION DEFERRED.
CLASS zcl_cust_access DEFINITION LOCAL FRIENDS ltcl_test_cust_acess.

CLASS ltcl_test_cust_acess DEFINITION FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
CLASS-DATA: go_osql TYPE REF TO if_osql_test_environment.
DATA: mo_cut TYPE REF TO zcl_cust_access.

CLASS-METHODS: class_setup.
CLASS-METHODS: class_teardown.

METHODS: setup.
METHODS: get_bill_type_space FOR TESTING.
METHODS: get_bill_type_true FOR TESTING.
METHODS: get_bill_type_false FOR TESTING.
ENDCLASS.

CLASS ltcl_test_cust_acess IMPLEMENTATION.
METHOD class_setup.
go_osql = cl_osql_test_environment=>create( i_dependency_list = VALUE #( ( ‘ZTEST_C’ ) ) ).
ENDMETHOD.

METHOD class_teardown.
go_osql->destroy( ).
ENDMETHOD.

METHOD setup.
DATA: lt_data TYPE TABLE OF ztest_c.
mo_cut = NEW #( ).
go_osql->clear_doubles( ).
lt_data = VALUE #(
( mandt = sy-mandt auart = ‘O1’ pstyv = ‘IT1’ fkart = ‘B1’ )
( mandt = sy-mandt auart = ‘O2’ pstyv = ‘IT2’ fkart = ‘B2’ )
).
go_osql->insert_test_data( lt_data ).

ENDMETHOD.

METHOD get_bill_type_space.
TRY.
mo_cut->get_bill_type(
EXPORTING
iv_auart = space
iv_pstyv = space
RECEIVING
rv_fkart = DATA(lv_fkart) ).
cl_abap_unit_assert=>assert_equals( act = lv_fkart
exp = space
msg = ‘Bill type is not blank’ ).
CATCH zcx_cust_error.
cl_abap_unit_assert=>assume_false(
EXPORTING
act = abap_false
msg = ‘select query executed’ ).
ENDTRY.

ENDMETHOD.

METHOD get_bill_type_true.
TRY.
mo_cut->get_bill_type(
EXPORTING
iv_auart = ‘O1’
iv_pstyv = ‘IT1’
RECEIVING
rv_fkart = DATA(lv_fkart) ).
cl_abap_unit_assert=>assert_equals( act = lv_fkart
exp = ‘B1’
msg = ‘Bill type is not found’ ).
CATCH zcx_cust_error.
cl_abap_unit_assert=>assume_false(
EXPORTING
act = abap_true
msg = ‘select query failed’ ).
ENDTRY.
ENDMETHOD.

METHOD get_bill_type_false.
TRY.
mo_cut->get_bill_type(
EXPORTING
iv_auart = ‘O2’
iv_pstyv = ‘IT1’
RECEIVING
rv_fkart = DATA(lv_fkart) ).
cl_abap_unit_assert=>assume_false(
EXPORTING
act = abap_false
msg = ‘select query success’ ).

CATCH zcx_cust_error.
cl_abap_unit_assert=>assert_equals( act = lv_fkart
exp = space
msg = ‘Bill type found’ ).
ENDTRY.

ENDMETHOD.

ENDCLASS.


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s