There is a wonderful blog Introduction to CDS Test Double Framework – How to write unit tests for ABAP CDS Entities? written by Sunil Bandameedapalli.

For me, the CDS view test double framework works as a magic for me: how the mocked data I inserted into the view under test could be read again in unit test code? As a result in this blog I will try to explain how CDS view framework works under the hood.

The view I am developed is listed below, which is simply used to return material guid with description by joining table MAKT with MARA:

@AbapCatalog.sqlViewName: 'PRODSHTEXT'
@VDM.viewType: #BASIC
@AccessControl.authorizationCheck: #NOT_REQUIRED
@ClientHandling.algorithm: #SESSION_VARIABLE
@EndUserText.label: 'Product Description'
define view ProductShortText
  as select from makt
    inner join   mara on makt.matnr = mara.matnr
{
  key mara.scm_matid_guid16 as ProductGuid,
  key makt.spras            as Language,
      makt.maktx            as ProductName,
      makt.maktg            as ProductNameLarge
}
Create a new class and you do not need to create any method within this class. Click tab “Local Test Classes”, and paste the source code from here.
Activate the class and perform unit test, you should see the information message that unit test is successfully executed.
Now let’s see how the whole scenario works.

Step1 – Test environment creation

This is done in CLASS_SETUP method:
cl_cds_test_environment=>create( i_for_entity =’PRODUCTSHORTTEXT’ ).
From the CASE-WHEN statement below we can know that the CDS test framework still supports various Database other than HANA.

Step2 – CDS view source code parse

The framework should know which database tables are used in the CDS view under test, since the mock data is inserted to database table level, not CDS view level.
The source code parse is done via a Visitor pattern and result stored in r_result, which contains two table, MARA and MAKT of course.
If you would like to know how this Visitor pattern works in detail, please refer to my blog Visitor pattern used in CDS View Test double framework.

Step3 – Created transient tables based on parsed database table

Since the test double framework knows from step2 that MARA and MAKT are involved in the CDS view under test, so now it is able to create transient tables based on both. The created tables are dummy, which is used to hold test data inserted in the unit test, and those dummy tables will be destroyed when the test environment is destroyed, I would like to call them as shadow table.

Step4 – unit test developers insert test data to shadow table

Unit test developers now prepare test data, wrap it by calling cl_cds_test_data=>create, and insert the wrapped test data into shadow table via API insert provided by test double framework.
In next step when the CDS view under test is queried in unit test code, the inserted data prepared in this step will be serving as response.

Step5 – OPEN SQL redirected to shadow table

When the CDS view is queried, the read operation will be actually redirected to shadow table created in step 3.
And how CDS test double framework knows which shadow table should be redirected for MARA and which for MAKT?
Actually in step3, when shadow table are created, the relationship between original table and created shadow table are maintained in an internal table:
Based on this metadata, the real redirection is switched on by a Kernel implementation:

Update on 2017-04-25 15:39PM

I remove the explanation on how the kernel module mentioned above is implemented as my ABAP colleague tells me it should not be published to the public.

 

To report this post you need to login first.

5 Comments

You must be Logged on to comment or reply to a post.

  1. Scott Zheng

    Hi Jerry,

    Really nice to meet you here. We are so proud of you, when we tried to find some articles about CDS, we could easily find you. And the key point is you are CHINESE!!

    We found you dig down the details in technical perspective, that is really fantastic!! Good for you.

    I also tried self study the CDS, however I encountered a problem, I tried to found solution in Google, but unfortunately I didn’t find it. At that moment, the one I really want to ask is you…

    I know this is not the right place to ask for your help. But I still hope you could help me with that. Hope it will not bother you too much.

    As below is the issue I got.

    I tried to create CDS view with below Code:

    @AbapCatalog.sqlViewName: 'ZXSZ_IFLIGHT'
    @ClientDependent: true
    @AccessControl.authorizationCheck: #NOT_REQUIRED
    @EndUserText.label: 'Flight'
    @Analytics.dataCategory: #CUBE
    @Analytics.dataExtraction.enabled: true
    @VDM.viewType: #BASIC
    define view ZXSZ_I_FLIGHT as select from sflight
    
    association [0..1] to ZXSZI_AIRLINE as _Airline
        on $projection.Airline = _Airline.Airline
    association [0..1] to ZXSZ_I_FLIGHTCONNECTION as _FlightConnection
        on $projection.Airline = _FlightConnection.Airline
        and $projection.FlightConnection = _FlightConnection.FlightConnection
    association [0..1] to ZXSZ_I_AIRCRAFTTYPE as _AircraftType
        on $projection.AircraftType = _AircraftType.AircraftType
    {
        @ObjectModel.foreignKey.association: '_Airline'
        key sflight.carrid          as Airline, 
        @ObjectModel.foreignKey.association: '_FlightConnection'
        key sflight.connid          as FlightConnection, 
        key sflight.fldate          as FlightDate, 
        @Semantics.amount.currencyCode: 'Currency'
        @DefaultAggregation: #MIN
        sflight.price           as FlightPrice, 
        @Semantics.currencyCode: true
        sflight.currency        as Currency, 
        @ObjectModel.foreignKey.association: '_AircraftType'
        sflight.planetype       as AircraftType, 
        @DefaultAggregation: #SUM
        sflight.seatsmax        as MaximumNumberOfSeats,
        @DefaultAggregation: #SUM
        sflight.seatsocc        as NumberOfOccupiedSeats,
        @DefaultAggregation: #SUM
        @Semantics.amount.currencyCode: 'Currency'
        sflight.paymentsum      as CurrentBookingsTotalAmount,
        /* Associations */
        _AircraftType, 
        _Airline, 
        _FlightConnection
    }

    After I activated this view, I got below error. And I’ve no idea how to fix it.

    System error in program CL_RS_BASE and form CREATE_DTAPRO_FROM_DDFIELD-0CURRENCY [Analytics] ZXSZ_I_FLIGHT (Data Definition) [GL3] ZXSZ_I_FLIGHT Unknown ABAP Syntax Check Problem

    Could you please help me with that when you are available?

     

    Thank you so much!!

    (0) 
    1. Jerry Wang Post author

      Hi Scott,

      I suggest you create a separate question and also paste the source code of your another view ZXSZI_AIRLINE, ZXSZ_I_FLIGHTCONNECTION and ZXSZ_I_AIRCRAFTTYPE there, and put the url of your created question via the comment to this blog, so that we can have a look at it.

      Best regards,

      Jerry

      (0) 
  2. Vincent Bai

    Nice blog! Just wondering which NetWeaver version CDS test double framework is available.

    Unfortunately, we’re currently on 7.40 seems it does not exist.

    (0) 

Leave a Reply