Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

ABAP Unit, how to use

Hi,

I created a small example of testing using ABAP Unit shown below.

But i get a syntax error in German, i translated to English and this is what i get

The reference to a test class (marking with FOR TESTING) is only in test classes possible

How do i resolve this?

CLASS myclass DEFINITION.

PUBLIC SECTION.

CLASS-DATA text TYPE string.

CLASS-METHODS set_text_to_x.

ENDCLASS.

CLASS myclass IMPLEMENTATION.

METHOD set_text_to_x.

text = 'U'.

ENDMETHOD.

ENDCLASS.

  • Test classes

CLASS mytest DEFINITION FOR TESTING.

PRIVATE SECTION.

METHODS mytest FOR TESTING.

ENDCLASS.

CLASS mytest IMPLEMENTATION .

METHOD mytest .

myclass=>set_text_to_x( ).

cl_aunit_assert=>assert_equals( act = myclass=>text

exp = 'X'

msg = 'failed').

ENDMETHOD.

ENDCLASS.

DATA my_ref TYPE REF TO mytest.

CREATE OBJECT my_ref type mytest.

call METHOD my_ref->mytest.

Former Member
replied

You're almost there. You don't need to create a object reference to your ABAP Unit Test class. To run the ABAP Unit test, you need to do the Program > Test > Unit Test

CLASS myclass DEFINITION.
PUBLIC SECTION.
CLASS-DATA text TYPE string.
CLASS-METHODS set_text_to_x.
ENDCLASS.

CLASS myclass IMPLEMENTATION.
METHOD set_text_to_x.
text = 'U'.
ENDMETHOD.
ENDCLASS.

* Test classes
*-------*
CLASS mytest DEFINITION FOR TESTING.
PRIVATE SECTION.
METHODS mytest FOR TESTING.
ENDCLASS.

CLASS mytest IMPLEMENTATION .
METHOD mytest .
myclass=>set_text_to_x( ).
cl_aunit_assert=>assert_equals( act = myclass=>text
exp = 'X'
msg = 'failed').
ENDMETHOD.

ENDCLASS.

Check thread

Regards,

Naimesh Patel

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question