Skip to content
OpenAgentSkill
Browsing is open · Catalog captured 2026-11-21 · Automatic updates and account services are paused.
Development

abap-unit-testing

Help with ABAP Unit testing including test class setup, assertions, test doubles, mocking frameworks, dependency injection, CDS test environments, SQL test environments, RAP BO test doubles, and test fixtures. Use when users ask about ABAP unit tests, test classes, test methods, CL_ABAP_UNIT_ASSERT, test doubles, mocking, CDS test environment, SQL test environment, RAP testing, ABAP test injection, test seams, behavior-driven testing, TDD in ABAP, test isolation, or writing automated tests for ABAP code. Triggers include "write a unit test", "create test class", "mock a dependency", "test a CDS view", "test a RAP BO", "test double", "assertion", "test fixture", "test isolation", or "ABAP unit".

likweitan · Static checks on file

Installation

Read the source and dependencies before running a command. Copying does not install or execute anything.

npx skills add likweitan/abap-skills --skill abap-unit-testing

About this skill


name: abap-unit-testing description: Help with ABAP Unit testing including test class setup, assertions, test doubles, mocking frameworks, dependency injection, CDS test environments, SQL test environments, RAP BO test doubles, and test fixtures. Use when users ask about ABAP unit tests, test classes, test methods, CL_ABAP_UNIT_ASSERT, test doubles, mocking, CDS test environment, SQL test environment, RAP testing, ABAP test injection, test seams, behavior-driven testing, TDD in ABAP, test isolation, or writing automated tests for ABAP code. Triggers include "write a unit test", "create test class", "mock a dependency", "test a CDS view", "test a RAP BO", "test double", "assertion", "test fixture", "test isolation", or "ABAP unit".

ABAP Unit Testing

Guide for writing effective ABAP Unit tests including test class setup, assertions, test doubles, mocking frameworks, and test environments for CDS, SQL, and RAP.

Workflow

  1. Determine the testing goal:

    • Testing business logic in a class method
    • Testing a CDS view entity
    • Testing a RAP BO behavior implementation
    • Testing database-dependent logic with SQL test doubles
    • Setting up test doubles for external dependencies
  2. Choose the right approach:

    • Direct unit test for pure logic (no dependencies)
    • Constructor/setter injection for mockable dependencies
    • CDS test environment for CDS view tests
    • OSQL test environment for SQL-dependent code
    • RAP BO test doubles for RAP behavior tests
  3. Follow the AAA pattern: Arrange → Act → Assert

  4. Ensure test isolation: Tests must not depend on persistent data or external systems

Test Class Fundamentals

Test Class Definition

"! Test class for ZCL_MY_CLASS
CLASS ltc_my_class DEFINITION FINAL FOR TESTING
  DURATION SHORT
  RISK LEVEL HARMLESS.

  PRIVATE SECTION.
    DATA cut TYPE REF TO zcl_my_class.  "Class Under Test

    CLASS-METHODS class_setup.    "Once before all tests
    CLASS-METHODS class_teardown. "Once after all tests
    METHODS setup.               "Before each test
    METHODS teardown.            "After each test

    METHODS test_calculate_total FOR TESTING.
    METHODS test_validate_input  FOR TESTING.
    METHODS test_empty_input     FOR TESTING RAISING cx_static_check.
ENDCLASS.

CLASS ltc_my_class IMPLEMENTATION.

  METHOD class_setup.
    " One-time setup for all tests in this class
  ENDMETHOD.

  METHOD class_teardown.
    " One-time cleanup
  ENDMETHOD.

  METHOD setup.
    " Create fresh instance before each test
    cut = NEW #( ).
  ENDMETHOD.

  METHOD teardown.
    " Cleanup after each test
  ENDMETHOD.

  METHOD test_calculate_total.
    " Arrange
    DATA(lv_quantity) = 5.
    DATA(lv_price) = CONV decfloat34( '10.50' ).

    " Act
    DATA(lv_result) = cut->calculate_total(
      iv_quantity = lv_quantity
      iv_price    = lv_price ).

    " Assert
    cl_abap_unit_assert=>assert_equals(
      act = lv_result
      exp = CONV decfloat34( '52.50' )
      msg = 'Total should be quantity * price' ).
  ENDMETHOD.

  METHOD test_validate_input.
    cl_abap_unit_assert=>assert_true(
      act = cut->validate_input( 'VALID_INPUT' )
      msg = 'Valid input should return true' ).
  ENDMETHOD.

  METHOD test_empty_input.
    TRY.
        cut->validate_input( '' ).
        cl_abap_unit_assert=>fail( msg = 'Should have raised exception' ).
      CATCH zcx_validation_error INTO DATA(lx_error).
        cl_abap_unit_assert=>assert_bound(
          act = lx_error
          msg = 'Exception should be raised for empty input' ).
    ENDTRY.
  ENDMETHOD.

ENDCLASS.

Test Class Attributes

AttributeOptionsPurpose
DURATIONSHORT / MEDIUM / LONGExpected execution time; SHORT < 1s (default for CI)
RISK LEVELHARMLESS / DANGEROUS / CRITICALImpact on system data; HARMLESS = no DB changes

Test Method Additions

AdditionPurpose
FOR TESTINGMarks method as a test method
RAISING cx_static_checkAllows exceptions to propagate (test fails on unhandled exception)

CL_ABAP_UNIT_ASSERT — Assertion Methods

MethodPurposeExample
assert_equalsValue equalityassert_equals( act = result exp = 42 )
assert_trueBoolean trueassert_true( act = lv_flag )
assert_falseBoolean falseassert_false( act = lv_flag )
assert_initialValue is initialassert_initial( act = lt_table )
assert_not_initialValue is not initialassert_not_initial( act = lt_result )
assert_boundReference is boundassert_bound( act = lo_instance )
assert_not_boundReference is not boundassert_not_bound( act = lo_ref )
assert_differsValues are differentassert_differs( act = val1 exp = val2 )
assert_char_cpCharacter pattern matchassert_char_cp( act = lv_text exp = '*error*' )
assert_char_npCharacter pattern
Read the complete source ↗