Code examples · Modern ABAP · Advanced
ABAP Unit with a test double: test business logic without the database
The problem
A discount service gives 10 percent to customers who spent at least 10,000, 5 percent from 1,000 and nothing below.
How we solve it
- 1ContractZIF_SPEND_READER
- 2Servicethe rule
- 3Test doublefake data
- 4TestsABAP Unit
The result
Four tests run in milliseconds and all pass, with no data in the database.
1. The requirement
A discount service gives 10 percent to customers who spent at least 10,000, 5 percent from 1,000 and nothing below. The rule must be tested at the boundaries, repeatedly and quickly, without any data in the database.
2. How we design it
The service must not read the database itself. It asks an object behind an interface, ZIF_SPEND_READER, for the customer's spend. In production that object is a class that runs a SELECT. In the tests it is a tiny double class that returns whatever number the test sets. The service receives the reader through its constructor, which is called dependency injection. A local test class sets the spend, calls the service and asserts the result.
Why we decided it this way
- 1An interface between logic and dataThe logic can be tested alone, and the data source can change without touching the rule.
- 2A hand-written double, not a frameworkIt is a few lines, needs no special API and is easy to read in the test.
- 3Constructor injection with a defaultProduction code creates the real reader automatically, while a test passes its own.
- 4Test the boundariesBugs sit at the limits: 999.99, 1000 and 10000 decide the tier, so each gets a test.
Before you start
- The table ZBOOKING from the ABAP SQL example (only the production reader uses it).
- ADT. Local test classes go into the 'Test Classes' tab of the global class.
3. The code, step by step
Step 1 of 4: The reader contract and the real reader
One interface, one production implementation.
" Interface
INTERFACE zif_spend_reader PUBLIC.
METHODS get_spend IMPORTING customer TYPE zbooking-customer
RETURNING VALUE(spend) TYPE decfloat34.
ENDINTERFACE.
" Production reader with SELECT SUM
CLASS zcl_spend_reader_db DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES zif_spend_reader.
ENDCLASS.
CLASS zcl_spend_reader_db IMPLEMENTATION.
METHOD zif_spend_reader~get_spend.
SELECT SUM( amount ) FROM zbooking
WHERE customer = @customer
INTO @DATA(total).
spend = total.
ENDMETHOD.
ENDCLASS.
Part by part (plain text)
- Interface. One method: give the customer, get the total spend. The type of the customer is taken from the table field, so both always agree.
- Production reader with SELECT SUM. The real reader sums the amounts of the customer in the database. If there are no rows the sum is empty and the result stays 0. Only this class touches the database, so only this class is hard to test.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
Four tests run in milliseconds and all pass, with no data in the database.
What you should see
ABAP Unit view (summary): ZCL_DISCOUNT_SERVICE 4 tests executed, 4 passed, 0 failed NO_DISCOUNT_JUST_BELOW_1000: passed FIVE_PERCENT_FROM_1000: passed TEN_PERCENT_FROM_10000: passed NO_DISCOUNT_FOR_NEW_CUSTOMER: passed
Try it and check the result
- Activate the interface, the reader, the service and then the test classes. Right-click the service class and choose Run As > ABAP Unit Test (Ctrl+Shift+F10).
- All four tests are green. The ABAP Unit view lists each test method by name.
- Break the rule on purpose: change 1000 to 1001 in the service and run again. The test five_percent_from_1000 fails and shows expected 5 and actual 0.
Common mistakes
- A test that reads the real database depends on its data and fails for the wrong reasons. Keep data access behind an interface and double it.
- Test the limits, not only a value in the middle of each range. Off-by-one errors live at the limits.
- Do not test the double. If a test passes only because the double returns what the test set, nothing real was checked: the line that counts is the rule in the service.
- The production reader adds amounts of different currencies as plain numbers. A real system would convert or group by currency first.