1

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?

jcjr
  • 1,503
  • 24
  • 40
  • Is all your code in an executable program? I.e. the 'main' class and the testclass as well? (Menu: Goto => Attributes / Popup => Field "Type") – József Szikszai Jul 13 '18 at 12:32
  • 2
    See docu: "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." – peterulb Jul 13 '18 at 12:42
  • 1
    @petul please convert your comment into an answer, this is probably the good answer (PS: it can be interesting to say that test seams exist since abap 7.50, too) – Sandra Rossi Jul 13 '18 at 18:48
  • @JozsefSzikszai: Yes, it is executable. I’ve omitted first line of code (REPORT). – jcjr Jul 16 '18 at 11:56
  • 1
    @jcjr it looks like that is the issue, as pointed out by petul as well. I think it only works this way: class created in SE24 and test class also there (Pushbutton Local test classes) – József Szikszai Jul 16 '18 at 13:19

1 Answers1

2

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.

peterulb
  • 2,869
  • 13
  • 20
  • My SAP version is 7.50 (if lower, error message would be different). It is executable program with this code. According to the error messages, there is no problem with SEAMs. How should I change the code or the ‘architecture’ to make it work? – jcjr Jul 16 '18 at 11:55
  • @jcjr You need a "real" class. Then navigate to the test include (SE80: Ctrl+Shift+F11, eclipse: Test Classes tab at the bottom) – peterulb Jul 16 '18 at 13:06
  • It works now! I would rather say "it has to be in a global class, not local". Both of them seem 'real' to me. Can you add it to your post to make it 100% clear? Anyway, your hint got me there. Thanks! – jcjr Jul 16 '18 at 13:32
  • @jcjr class pools in ABAP actually means global class. But I can add it for clarification – peterulb Jul 16 '18 at 19:05