ERPClimb logoERPClimb
Modularization
ABAP Developmentadvanced 10 min read

Modularization Part 10: Modularization for Unit Testing and Regression Safety

Preview · a free account unlocks the full lesson

Key takeaway

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 sections

Visual overview

Modularization Part 10: Modularization for Unit Testing and Regression Safety — learning sheetFlow: Add the local test class to ZCL_FLIGHT_BOOKING_SERVICE (Local Test Classes include) → Run the unit tests (Ctrl+Shift+F10) → Break the seat check on purpose and watch a test fail → Include ABAP Unit in your ATC check variant so tests run before release. Key ideas: WHY UNIT TESTS; WHAT MAKES CODE TESTABLE; THE DAO INTERFACE AND A TEST DOUBLE; ANATOMY OF AN ABAP UNIT TEST CLASS. Watch out: A FORM that reads the database, calculates and writes a list in one block cannot be unit-tested.ERPClimb.com / ABAP / LESSON 10/10 / LEARNING SHEETModularization Part 10: Modularization for Unit Testingand Regression Safety1 · HOW IT FLOWS1Add the localtest class toZCL_FLIGHT_BOOKING_SERVICE…2Run the unittests(Ctrl+Shift+F10)3Break the seatcheck on purposeand watch a test…4Include ABAPUnit in your ATCcheck variant so…2 · KEY IDEAS EXPLAINEDWhy Unit TestsEvery change to a report or FM risks breakingexisting behaviour (regression). Manual retestingis slow and incomplete. Unit tests run in seconds…What Makes Code TestableSmall units with clear inputs and outputs(methods, FMs with typed parameters).No hidden dependencies: database, time (sy-datum),user (sy-uname) passed in or behind interfaces.The Dao Interface And A Test DoubleThe service receives a ZIF_FLIGHT_DAO in itsconstructor. Production passes ZCL_FLIGHT_DAO(real SELECTs). The test passes LCL_FAKE_DAO…Anatomy Of An ABAP Unit Test ClassFOR TESTING — only runs in test mode, never inproduction.RISK LEVEL HARMLESS — does not change persistentdata.3 · REMEMBER · WATCH OUTRememberCode is testable when business logic does not directly SELECT orUPDATE — it calls a DAO interface.In a test, you pass a fake DAO (test double) that returns fixed data;no database needed.ABAP Unit tests are local classes FOR TESTING, run with Ctrl+Shift+F10in SE80/ADT or from ATC.!Watch outA FORM that reads the database,calculates and writes a list in oneblock cannot be unit-tested.4 · IN A REAL PROJECTA team changed the seat availability rule in a booking FM during a support fix and accidentally inverted acomparison. Manual testing only covered the happy path, so overbookings went live and had to be correctedmanually for two days. They then refactored the rule into a service class with a DAO interface and added ABAP…Original ERPClimb learning sheet • Verify against your SAP release and edition

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.

Free account

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

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.