SAP OABAP Programming using Test-seam and Test-injection

Estimated read time 6 min read

I am happy to share my very first blog post on SAP OABAP Programming using Test-seam and Test-injection. In this blog post you will learn how to use a Test-seam and Test-injection

Test-seam and Test-injection 

Test-seam 

Seam inserted between the statements seam and end-seam in production source code  and which can be replaced by an injection when module tests are run. 

What is ABAP Test Seam? 

Scope of use : Writing test cases Test seam is an easy way to replace or extend source code in the production part of your program. 

Replacement level 

In method code 

Test Injection life cycle  

Replacement works until the end of the test method that defines the injection 

Features of Test Seam 

Test seam is ignored in production use of the program A program can contain multiple test seams Multiple test injection can be defines for a single test seam Test seam is available for all executable units of the main program including tests including local classes and subroutines Unit tests can do test injection when executing test methods or setup methods If injections are repeated in the same test seam the last injection is the active injection 

Limitation of  Test seam 

Injections can only be created in test classes defined in the test included 

Scenarios used by test seam 

Authorization checks Reading persistent data Modifying persistent data Creating test doubles 

Test seam 

Injection 

TEST-SEAM authorization seam 

AUTHORITY-CHECK OBJECT ‘S_CTS_ADMI’ 

ID ‘CTS_ADMFCT’ 

FIELD ‘TABL’. 

END-TEST-SEAM 

IF sy-subrc = 0 

Is_authorized = abap_true. 

ENDIF. 

TEST-INJECTION 

authorization 

seam 

Sy-subrc = 0. 

END-TEST-INJECTION 

 

1. Created a class it’s having method called fetch data   

2. We need to create the local test class. 

 

  

 

 

 

 

 

 

 

 

 

 

 

3. While providing the session breakpoints in the test class we can debug the ABAP unit class  

4. If we click on coverage it will navigate to the debugging screen 

5.In the ev_mara there no data will be present if we execute the code line by line  

 

6.It will go to the development code

 

7.If you executed the code in the test-seam it will go to the test-injection 

 

8.There am mocking the data for the table by that ev_mara parameters will get the data 

9.Now the select query is successful so it’s entering sy-subrc condition 

10.Once the code is done we need to check the coverage . 

Refer the code Below 

 

Refer the code Below

METHOD fetch_data.
TEST-SEAM mara.
SELECT matnr mtart FROM mara INTO CORRESPONDING FIELDS OF TABLE ev_mara WHERE matnr = iv_matnr.
END-TEST-SEAM.
IF sy-subrc = 0.
MESSAGE ‘Record found’ type ‘S’.
ELSE.
MESSAGE ‘Record not found’ type ‘E’.
ENDIF.

ENDMETHOD.

*”* use this source file for your ABAP unit test classes
CLASS lcl_test DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PUBLIC SECTION.
METHODS : _fetch_data FOR TESTING.
PRIVATE SECTION.
METHODS : setup ,
teardown.
DATA : lo_fcut TYPE REF TO zsh_cl_test_seam1.

ENDCLASS.
CLASS lcl_test IMPLEMENTATION.
METHOD setup.
CREATE OBJECT lo_fcut.
ENDMETHOD.
METHOD teardown.
FREE : lo_fcut.
ENDMETHOD.
METHOD _fetch_data.
DATA : ev_mara TYPE zsh_cl_test_seam1=>tt_mara,
lv_matnr TYPE matnr.
lv_matnr = ‘CM-FL-V00’.
“=======Mocking the data using test injection.
TEST-INJECTION mara.
ev_mara = VALUE #( ( matnr = ‘CM-FL-V00’ mtart = ‘KMAT’ )
( matnr = ‘CM-FL-V01’ mtart = ‘KMAT’ )
( matnr = ‘CM-MLFL-KM-VXX’ mtart = ‘KMAT’ ) ).

END-TEST-INJECTION.
lo_fcut->fetch_data(
EXPORTING
iv_matnr = lv_matnr ” Material Number
IMPORTING
ev_mara = ev_mara
).
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = ev_mara ” Data object with current value
exp = ev_mara ” Data object with expected type
).
ENDMETHOD.
ENDCLASS.

 

 

We have a standard class for test seam and injection

CL_AU_SAMPLE_TEST_SEAMS 

 

 

​ I am happy to share my very first blog post on SAP OABAP Programming using Test-seam and Test-injection. In this blog post you will learn how to use a Test-seam and Test-injection. Test-seam and Test-injection Test-seam Seam inserted between the statements seam and end-seam in production source code  and which can be replaced by an injection when module tests are run. What is ABAP Test Seam? Scope of use : Writing test cases Test seam is an easy way to replace or extend source code in the production part of your program. Replacement level In method code Test Injection life cycle  Replacement works until the end of the test method that defines the injection Features of Test Seam Test seam is ignored in production use of the program A program can contain multiple test seams Multiple test injection can be defines for a single test seam Test seam is available for all executable units of the main program including tests including local classes and subroutines Unit tests can do test injection when executing test methods or setup methods If injections are repeated in the same test seam the last injection is the active injection Limitation of  Test seam Injections can only be created in test classes defined in the test included Scenarios used by test seam Authorization checks Reading persistent data Modifying persistent data Creating test doubles Test seam Injection TEST-SEAM authorization seam AUTHORITY-CHECK OBJECT ‘S_CTS_ADMI’ ID ‘CTS_ADMFCT’ FIELD ‘TABL’. END-TEST-SEAM IF sy-subrc = 0 Is_authorized = abap_true. ENDIF. TEST-INJECTION authorization seam Sy-subrc = 0. END-TEST-INJECTION  1. Created a class it’s having method called fetch data   2. We need to create the local test class.               3. While providing the session breakpoints in the test class we can debug the ABAP unit class  4. If we click on coverage it will navigate to the debugging screen 5.In the ev_mara there no data will be present if we execute the code line by line   6.It will go to the development code 7.If you executed the code in the test-seam it will go to the test-injection  8.There am mocking the data for the table by that ev_mara parameters will get the data 9.Now the select query is successful so it’s entering sy-subrc condition 10.Once the code is done we need to check the coverage . Refer the code Below  Refer the code Below

METHOD fetch_data.
TEST-SEAM mara.
SELECT matnr mtart FROM mara INTO CORRESPONDING FIELDS OF TABLE ev_mara WHERE matnr = iv_matnr.
END-TEST-SEAM.
IF sy-subrc = 0.
MESSAGE ‘Record found’ type ‘S’.
ELSE.
MESSAGE ‘Record not found’ type ‘E’.
ENDIF.

ENDMETHOD.

*”* use this source file for your ABAP unit test classes
CLASS lcl_test DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PUBLIC SECTION.
METHODS : _fetch_data FOR TESTING.
PRIVATE SECTION.
METHODS : setup ,
teardown.
DATA : lo_fcut TYPE REF TO zsh_cl_test_seam1.

ENDCLASS.
CLASS lcl_test IMPLEMENTATION.
METHOD setup.
CREATE OBJECT lo_fcut.
ENDMETHOD.
METHOD teardown.
FREE : lo_fcut.
ENDMETHOD.
METHOD _fetch_data.
DATA : ev_mara TYPE zsh_cl_test_seam1=>tt_mara,
lv_matnr TYPE matnr.
lv_matnr = ‘CM-FL-V00’.
“=======Mocking the data using test injection.
TEST-INJECTION mara.
ev_mara = VALUE #( ( matnr = ‘CM-FL-V00’ mtart = ‘KMAT’ )
( matnr = ‘CM-FL-V01’ mtart = ‘KMAT’ )
( matnr = ‘CM-MLFL-KM-VXX’ mtart = ‘KMAT’ ) ).

END-TEST-INJECTION.
lo_fcut->fetch_data(
EXPORTING
iv_matnr = lv_matnr ” Material Number
IMPORTING
ev_mara = ev_mara
).
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = ev_mara ” Data object with current value
exp = ev_mara ” Data object with expected type
).
ENDMETHOD.
ENDCLASS.

  We have a standard class for test seam and injectionCL_AU_SAMPLE_TEST_SEAMS     Read More Application Development Blog Posts articles 

#SAP

You May Also Like

More From Author

+ There are no comments

Add yours