ERPClimb logoERPClimb
OOP ABAP
ABAP Developmentadvanced 12 min read

OOP ABAP Part 9: Dependency Injection and ABAP Unit

Preview · a free account unlocks the full lesson

Key takeaway

Make classes testable with dependency injection: constructor injection, optional parameters with defaults, injection through a factory, and ABAP Unit test doubles written by hand or generated with CL ABAP TESTDOUBLE.

In this lessonShow sections

Visual overview

OOP ABAP Part 9: Dependency Injection and ABAP Unit — learning sheetFlow: Run the tests in the code example (Ctrl+Shift+F10) → Change the discount threshold in lcl_order_service and watch a test fail → Replace the hand-written stub with CL_ABAP_TESTDOUBLE as in the commented section. Key ideas: THE PROBLEM; CONSTRUCTOR INJECTION; OPTIONAL INJECTION WITH A DEFAULT; INJECTION VIA FACTORY / INJECTOR. Watch out: A class that does CREATE OBJECT of its own DAO or calls static SELECT helpers cannot be isolated in tests.ERPClimb.com / ABAP / LESSON 9/10 / LEARNING SHEETOOP ABAP Part 9: Dependency Injection and ABAP Unit1 · HOW IT FLOWS1Run the tests in the codeexample (Ctrl+Shift+F10)2Change the discountthreshold inlcl_order_service and…3Replace the hand-writtenstub withCL_ABAP_TESTDOUBLE as in…2 · KEY IDEAS EXPLAINEDThe ProblemMETHOD constructor. CREATE OBJECT mo_dao TYPEzcl_order_dao. ENDMETHOD.Constructor InjectionProduction code (or a factory) passeszcl_order_dao.The test passes ltd_order_dao (a test double).Optional Injection With A DefaultExisting callers should not change. Make theparameter OPTIONAL; in the constructor: IF io_daoIS BOUND use it, ELSE create the real one. Callers…Injection Via Factory / InjectorFor deep call chains, the factory holds a staticattribute for the instance to return. A test-onlyinjector class (FRIENDS of the factory, FOR…3 · REMEMBER · WATCH OUTRememberDependency injection (DI): a class receives the objects it needs (DAO,logger, tax calculator) instead of creating them itself.Constructor injection is the clearest form: dependencies are IMPORTINGparameters of the constructor, typed as interfaces.In ABAP Unit, you inject test doubles — hand-written local classes orgenerated with CL_ABAP_TESTDOUBLE.!Watch outA class that does CREATE OBJECT ofits own DAO or calls static SELECThelpers cannot be isolated in tests.4 · IN A REAL PROJECTA discount class created its own DAO internally, so its tests used real sales orders from the developmentsystem. After a client copy the orders changed, all tests failed, and the team started ignoring the testresults. They added an optional constructor parameter for the DAO, wrote a small spy double and rewrote the…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Part 8 of this series created objects inside factories; now it's time to look at the consumer side — how a class receives those objects, and how tests pass in fakes instead. (Part 10 of the Modularization series introduced the basics of ABAP Unit; this lesson focuses on the object-oriented design side of the same idea.) Dependency injection means a class receives the objects it needs — a DAO, a logger, a tax calculator — instead of creating them itself.

The problem without it looks like this: METHOD constructor. CREATE OBJECT mo_dao TYPE zcl_order_dao. ENDMETHOD. — the class is hard-wired to real database access, and testing it would require specific data to exist in the database, which changes over time and differs between systems.

Constructor injection is the clearest form of the fix: METHODS constructor IMPORTING io_dao TYPE REF TO zif_order_dao. Production code, or a factory, passes in zcl_order_dao, while a test passes in ltd_order_dao, a test double; either way, the class itself only ever knows the interface.

To avoid breaking existing callers, injection can be made optional with a default: make the parameter OPTIONAL, and inside the constructor, write IF io_dao IS BOUND, use it, ELSE create the real one. Existing callers stay fully compatible, while tests can still inject their own double.

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

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.