Show TOC

Procedure documentationHandling Exceptions in ABAP Unit Tests Locate this document in the navigation structure

Procedure

How should your ABAP Unit tests handle exceptions from the code under test?

First of all, ABAP Unit captures unexpected exceptions, even non-catchable exceptions. If an exception occurs, the ABAP unit test fails and the exception is reported as the cause of the failure.

If a method under test declares an exception in its signature, then the test method can allow the exception to propagate to the ABAP Unit runtime. It is best that the test method declares the exception so that the possible behavior of the method is clear. The test method should not catch the exception. .

Syntax Syntax

  1. METHODS test_a_method FOR TESTING
      RAISING cx_sy_zerodivide.
    ...
    METHOD test_a_method.
    ...
    * Method under test declares 
    * CX_SY_ZERODIVIDE in its signature...
    * The exception is passed to the ABAP 
    * Unit runtime, if it occurs...
      cut->divide_by_zero( nominator = 5 ).
    ...
    ENDMETHOD.
    
End of the code.

If you are provoking exceptions in the code under test, then you can use the FAIL method of CL_ABAP_UNIT_ASSERT to report a test failure.

Syntax Syntax

  1. * In a test method, you catch 
    * a provoked exception
    TRY.
        cut->divide_by_zero( denominator = 1).
      CATCH cx_sy_zerodivide.
    ENDTRY.
    	cl_abap_unit_assert=>fail(
      msg = 'CX_SY_ZERODIVIDE not raised'
      level = if_aunit_constants=>critical ).
    
End of the code.