ABAP - Keyword Documentation →  ABAP - Reference →  program editing →  Testing and Checking Programs →  ABAP Unit → 

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 by the statements of an injection in a test class of the same program.

Test seams have the following properties:

Note

Test seams are a simple way of replacing or expanding source code in 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:

Example for authorization checks

An injection can use 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.
  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.

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. Using test seams, unit tests can bypass the dependencies of persistent data by replacing it with constructed data.

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

Example for changing persistent data

When they run, unit tests must not modify production content of database tables or other repositories. Using test seams, unit tests can record the operands of modifying database operations or compare actual changes with expected 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.
  MODIFY sflight
    FROM TABLE @new_flights.
END-TEST-SEAM.
TEST-INJECTION store_content_seam.
  cl_abap_unit_assert=>assert_equals(
    act = new_flights
     exp = global_buffer=>exp_flights ).
END-TEST-INJECTION.

Example for a test double

In the following source code section, the production source text of class that is dependent on database content is instantiated. The unit test injects the instantiated test double at the location of the production object.

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

Example

See also the class CL_AU_SAMPLE_TEST_SEAMS in the package SABP_UNIT_SAMPLE.



Continue
TEST-SEAM
TEST-INJECTION