Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
sowjanya_hr
Explorer
1,154

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 

 

sowjanya_hr_0-1720436076425.png

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

sowjanya_hr_1-1720436076428.png

2. We need to create the local test class. 

sowjanya_hr_2-1720436076431.png

 

  

 

 

 

 

 

 

 

 

 

 

 

3. While providing the session breakpoints in the test class we can debug the ABAP unit class  sowjanya_hr_3-1720436076433.png

sowjanya_hr_4-1720436076435.png

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

sowjanya_hr_5-1720436076439.png

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

sowjanya_hr_6-1720436076440.png

 

6.It will go to the development codesowjanya_hr_7-1720436076444.png

 

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

sowjanya_hr_8-1720436076447.png

 

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

sowjanya_hr_9-1720436076448.pngsowjanya_hr_10-1720436076451.png

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 . 

sowjanya_hr_11-1720436076453.png

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 

 

4 Comments
Mohanreddy
Explorer
0 Kudos

Good information, Thank you.

Sandra_Rossi
Active Contributor

For information, test seams are not liked a lot by "modern" developers because test seams contain non-productive code and it obfuscates the code, dependency injection is preferred, so use test seams only if no other choice.

vulturas
Explorer

Please make use of standard principles for unit tests in OOP.

Your approach mixes productive coding with test code - which is a very bad idea and is kinda out of date.

Also this approach violates many (even all?) OO principles and clean code/architecture approaches.

matt
Active Contributor
0 Kudos

Test seams should only ever be used in legacy code to create unit tests, before you refactor the code to bring it to modern standards. A program with test seams should NEVER be transported out of development.

Labels in this area