问题
I’m trying to use TEST-INJECTION
and TEST-SEAM
in my code. I have following code:
CLASS lcl_undertest DEFINITION.
PUBLIC SECTION.
CLASS-METHODS mymethod RETURNING VALUE(r) TYPE string.
ENDCLASS.
CLASS lcl_undertest IMPLEMENTATION.
METHOD mymethod.
TEST-SEAM vypis.
r = 'abc'.
END-TEST-SEAM.
ENDMETHOD.
ENDCLASS.
CLASS ltc_testclass DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT FINAL.
PRIVATE SECTION.
METHODS test_method1 FOR TESTING.
ENDCLASS.
CLASS ltc_testclass IMPLEMENTATION.
METHOD test_method1.
TEST-INJECTION vypis.
r = 'xyz'.
END-TEST-INJECTION.
DATA(res) = lcl_undertest=>mymethod( ).
cl_abap_unit_assert=>assert_equals(
act = res
exp = 'xyz'
msg = 'nespravny text'
).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
WRITE / lcl_undertest=>mymethod( ) .
For these lines
TEST-INJECTION vypis.
r = 'xyz'.
END-TEST-INJECTION.
these three following errors are indicated:
- No injection is expected here.
- Field "R" is unknown.
- Incorrect nesting: For the statement "END-TEST-INJECTION", there is no open structure introduced by "TEST-INJECTION".
I’ve also tried to copy some example codes from documentation and blogs, but there were same errors returned.
What is the reason of problems?
回答1:
Please see the documentation regarding test-seams here.
Note
Injections can only be created in test classes that are defined in a test include of the current program. Test includes are currently only possible in class pools and function groups. This means that test seams are only feasible in class pools and function groups.
Also be aware that they only exist since ABAP release 7.50 (thanks to Sandra)
To make it clear: the documentation speaks of class pools, meaning that it won't work in e.g. reports. You'll have to declare a global class with the local test class defined in its test include.
来源:https://stackoverflow.com/questions/51324411/test-injection-and-test-seam-in-abap-code