METHODS - FOR TESTING

Short Reference

Syntax

METHODS meth [ABSTRACT|FINAL]
  FOR TESTING
  [RAISING exc1 exc2 ...].


Effect

This statement is only possible in a test class. It declares a test method that is called during an ABAP unit test. A test can be programmed in the method implementation. The static methods of the class CL_AUNIT_ASSERT are used to evaluate the test results.

For the ABSTRACT, FINAL and RAISING additions, the same applies as for general instance methods.

Note

Test methods should be protected privately and in the inheritance of methods. Since test classes implicitly offer friendship to the test driver of the runtime environment, this can call the test classes. Test methods only need to be public in rare cases in which a test is to execute tests from other test classes.

Example

: Definition of a test class mytest using a test method mytest, which checks the value of the attribute text after the method mymethod of the class myclass has been called. In this example, an ABAP Unit Test reports an error, because the value "X" is expected instead of "U".

* Productive classes

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' ).
  ENDMETHOD.
ENDCLASS.