Lesson 5 of 5
Testing CDS view entities
10 min
Listen to this lesson
A CDS view is code. Its joins, CASE expressions and aggregations can be wrong in the same ways an ABAP method can, and they deserve unit tests just as much. The challenge is data: a view reads tables, and the contents of CMD's tables change daily. SAP's answer is the CDS test double framework, which redirects the view's data sources to test doubles you fill with exactly the rows each test needs.
- class_setupCall create on CL_CDS_TEST_ENVIRONMENT for the view under test
- setupClear doubles so each test starts empty
- Arrangeinsert_test_data with a few rows for each data source
- ActSELECT from the view with ABAP SQL
- AssertCL_ABAP_UNIT_ASSERT on totals, flags and row counts
- class_teardowndestroy the environment
How the framework works
SAP's ABAP Data Models guide explains that when data access in your code under test goes through CDS view entities, the framework redirects the entity's reads to doubled database tables. You create the environment once per test class for the entity under test, insert test data into the doubles of its dependencies, select from the view and assert the result. SAP's ABAP Unit cheat sheet shows the pattern with CL_CDS_TEST_ENVIRONMENT: create in class_setup, clear_doubles in setup, insert_test_data in each test and destroy in class_teardown. For CDS table functions and scalar functions implemented as AMDP, SAP points to the AMDP test double framework instead.
| Dependency to isolate | Framework or class | Typical use |
|---|---|---|
| CDS view entity logic | CDS test double framework (CL_CDS_TEST_ENVIRONMENT) | Test a view’s joins, CASE logic and aggregations |
| ABAP SQL in a class | ABAP SQL test double framework (CL_OSQL_TEST_ENVIRONMENT) | Test a method that selects from tables or views |
| AMDP-based CDS functions | AMDP test double framework | Test table functions and scalar functions |
| RAP business object | RAP BO test double framework | Test code that calls a RAP BO through EML |
Test double frameworks SAP documents for data-related dependencies.
What to test in a view
Test the logic you wrote, not SAP HANA. For Contoso's item view, that means: TotalDeposit equals quantity times deposit; an item with zero quantity gives zero; the per-customer aggregation adds items across agreements; the IsExpired flag flips on the right date boundary; and the many-to-one customer association does not duplicate rows. Each case needs only two or three rows of test data, which is exactly what doubles make easy. Real data in CMQ is still needed for the business acceptance test, but it is the wrong tool for proving each rule.
Write the expected values by hand from the test rows, not by running the view and copying its output; a test that copies the current result only proves the view does what it already does. Keep each test small enough that a failing assertion points straight at one rule, and name the test method after that rule, for example zero_quantity_gives_zero. Once the test class is in the same package as the view, it runs whenever someone runs unit tests for the package and in any ATC or unit test run attached to the transport.
What would you do?
Kenji's developer proposes testing the deposit total view by selecting from it in CMD and comparing to numbers he noted from last week's data. What should Kenji suggest instead?
- CDS views contain logic and need unit tests.
- The CDS test double framework redirects a view’s data sources to doubles.
- Pattern: create in class_setup, clear_doubles in setup, insert_test_data per test, destroy at the end.
- Use the ABAP SQL test double for code that selects data, and the AMDP framework for table functions.
- Unit tests prove rules; access control and business acceptance need their own tests.
Check your understanding · not graded
Lesson check
1. Which class creates the environment for CDS test doubles?
2. Which framework does SAP point to for testing CDS table functions implemented as AMDP?
3. Why are test doubles better than live CMD data for unit testing a view?
4. Where in the test class is clear_doubles typically called?