TEST-INJECTION and TEST-SEAM in ABAP code

一笑奈何 提交于 2020-01-05 09:21:48

问题


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:

  1. No injection is expected here.
  2. Field "R" is unknown.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!