Ansicht
Dokumentation

ABENTEST_SEAMS - TEST SEAMS

ABENTEST_SEAMS - TEST SEAMS

SUBST_MERGE_LIST - merge external lists to one complete list with #if... logic for R3up   BAL Application Log Documentation  
This documentation is copyright by SAP AG.
SAP E-Book

Test Seams

Test seams are language constructs designed especially for unit tests and are implemented using the following statements:

Defines a test seam as a replaceable area in the production code of a program.
Replaces the executable statements of a test seam with the statements of an injection in a test class of the same program.

Test seams have the following properties:

  • Test seams do not affect the use of programs in production. No injection takes place, rather the original code is executed.
  • A program can contain multiple test seams.
  • Multiple injections can be defined for a single test seam.
  • Injections can only be created in test classes that are defined in a test include. Test seams can be used in all executable units of a master program that includes a test include, including methods of local classes and subroutines.
  • Unit tests can make injections while test methods or the setup method are being executed.
  • If injections are repeated in the same test seam, the last injection is performed as an active injection.

Notes

  • Test seams are a simple way of replacing or expanding source code in the production parts of a program for test purposes. If, for example, the behavior of certain statements prevents tests from running, the unit test can replace them with suitable alternatives. Typical scenarios are:
  • Authorization checks

  • Reading persistent data

  • Modifying persistent data

  • Creating test doubles

  • Test seams are intended mainly for use with legacy code that, due to insufficient separation of concerns, is not suitable for unit tests. New programs, on the other hand, should be modularized in such a way that test seams are made unnecessary.

Example for Authorization Checks

An injection can replace the statement AUTHORITY-CHECK to bypass the dependency of a unit test on the authorizations of the current user by setting a suitable return code.

Test Seam Injection
TEST-SEAM authorization_seam.\lbr  AUTHORITY-CHECK OBJECT 'S_CTS_ADMI'\lbr      ID 'CTS_ADMFCT' FIELD 'TABL'.\lbrEND-TEST-SEAM.\lbr\lbrIF sy-subrc = 0.\lbr  is_authorized = abap_true.\lbrENDIF. TEST-INJECTION authorization_seam.\lbr  sy-subrc = 0.\lbrEND-TEST-INJECTION.

Example for Reading Persistent Data

It is often not possible to make assumptions about the content of database tables or other repositories in unit tests. Test seams enable unit tests to bypass the dependencies of persistent data by replacing it with self-constructed data.

Test Seam Injection
TEST-SEAM read_content_seam.\lbr  SELECT *\lbr         FROM sflight\lbr         WHERE  carrid IN @carrid_range AND\lbr fldate EQ @sy-datum\lbr      INTO TABLE @flights.\lbrEND-TEST-SEAM. TEST-INJECTION read_content_seam.\lbr  flights =\lbr    VALUE #( ( carrid = 'LHA'\lbr               connid = 100 )\lbr ( carrid = 'AFR'\lbr               connid = 900 ) ).\lbrEND-TEST-INJECTION.

Example for Changing Persistent Data

The execution of unit tests must not modify content of database tables or other repositories used in production. Using test seams, unit tests can record the operands of modifying database operations or compare them with expected changes instead of actual changes. In the following source code section compares, the injection compares the change values with a public static attribute.

Test Seam Injection
TEST-SEAM store_content_seam.\lbr  MODIFY sflight\lbr    FROM TABLE @new_flights.\lbrEND-TEST-SEAM. TEST-INJECTION store_content_seam.\lbr  cl_abap_unit_assert=>assert_equals(\lbr act = new_flights\lbr     exp = global_buffer=>exp_flights ).\lbrEND-TEST-INJECTION.

Example for a Test Double

In the following source code section, the production source code of a class that depends on database content is instantiated. The unit test injects the instantiated test double instead of the production object.

Test Seam Injection
TEST-SEAM instantiation_seam.\lbr  me->oref = NEW #( ).\lbrEND-TEST-SEAM. TEST-INJECTION instantiation_seam.\lbr me->oref = NEW dummy_class( ).\lbr END-TEST-INJECTION.

Example

See also the class CL_AU_SAMPLE_TEST_SEAMS in the package SABP_UNIT_SAMPLE.






Addresses (Business Address Services)   General Material Data  
This documentation is copyright by SAP AG.

Length: 7491 Date: 20240424 Time: 155136     sap01-206 ( 78 ms )