Modularization Part 10: Modularization for Unit Testing and Regression Safety
Preview · a free account unlocks the full lesson
Make modular code testable: separate logic from database access through an interface, replace the DAO with a test double, and write ABAP Unit tests (FOR TESTING, setup, cl abap unit assert) that protect you from regressions.
In this lessonShow sectionsHide sections
Visual overview
Explanation
Part 9 of this series built ZCL_FLIGHT_BOOKING_SERVICE around the interface ZIF_FLIGHT_DAO; this lesson shows why that design pays off — it can be tested automatically. Every change to a report or function module risks breaking existing behavior, and manual retesting is slow and incomplete, whereas unit tests run in seconds, repeatedly, before every transport.
Code becomes testable when it's built from small units with clear inputs and outputs, such as methods or function modules with typed parameters; when it has no hidden dependencies — the database, the current date (sy-datum), or the current user (sy-uname) get passed in or sit behind interfaces instead of being read directly; and when business rules are separated from UI output (WRITE or ALV) and from database access.
Classic FORM routines that rely on global data are hard to test this way, so the first step is usually to move the logic into a class method. A FORM that reads the database, calculates, and writes a list all in one block simply cannot be unit-tested as it stands.
The design that makes this possible is the DAO interface together with a test double. The service receives a ZIF_FLIGHT_DAO through its constructor; in production, it's handed ZCL_FLIGHT_DAO, which performs the real SELECTs, while a test hands it LCL_FAKE_DAO instead, which returns a flight with, say, exactly 10 free seats — precisely the situation the test wants to exercise, with no database needed at all.
Create a free account to keep reading
You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.
- Full explanation
- Code example
- Real project scenario
- Common mistakes
- Best practices
- Interview angle
One tap with Google works too. Every topic keeps its first lesson open to everyone.
Keep exploring
Interview questions on this topic
Related SAP topics
Referenced SAP objects
Reviewed reference pages for the objects this page mentions.
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.