cancel
Showing results for 
Search instead for 
Did you mean: 

Creating unit test data in the RAP context

01-02-2025 5:44 PM
felixw Explorer
2109 views 5 comments Go to solution
0 Likes
SAP Managed Tags
Labels
unit_test
Subscribe

Dear community,

I am having an error creating a unit test with RAP. I have the following developments objects:

  • Z_A_ENTITY_A => My database table for entity A
  • Z_R_ ENTITY_A => My CDS base view for entity A
  • Z_I_ENTITY_A => My CDS interface view for entity A
  •  Z_C_ENTITY_A => My CDS consumption view for entity A
  • The same objects for an entity B. Entity A has a 0…n relationship to entity B.

Now I want to create a simple unit test for an determination of the interface view in which test data for entity A and B is necessary. In my test class I create the test environment like the following:

 

 

 

cds_test_environment = cl_cds_test_environment=>     cds_test_environment = cl_cds_test_environment=>create_for_multiple_cds(
                               VALUE #(
                                       ( i_for_entity = 'Z_I_ENTITY_A' )
                                       ( i_for_entity = 'Z_I_ENTITY_B')
                                     )
                             ).

DATA my_mock_data TYPE STANDARD TABLE OF Z_A_ENTITY_A.
" Fill my_mock_data with test data values
...
" Insert test data
cds_test_environment->insert_test_data( i_data = my_mock_data ).

 

 

 

The insert_test_data statement fails with the following message “The test double ‘Z_A_ENTITY_A’ not found.

When I change the create_for_multiple_cds statement to use the base views, it works. However, I need to test on interface view level. How is this possible? What can I change in order to enable this?

In the documentation of create_for_multiple_cds it says "This parameter should not be used while writing a unit test. For unit test, the framework will automatically evaluate the first level dependencies in the hierarchy and will create doubles for them". This seems like an explanation why it works when creating the test environment on base view level. However, all attempts from my side to fill the dependecy list manually failed. Also, to me it is not clear whether "hierarchy" here refers to the relationship between Entities (like Entity A and B) or the relationship between base views and consumption views.

Any help is highly appreciated!

 

 

0 Likes
View Entire Topic
felixw
Explorer

I was finally able to solve the problem without by-passing the base view.

@MioYasutakeYou were right to set the parameter i_select_base_dependencies = abap_true. The error I did all along was that I set it only for Entity A not also entity B.

For completness sake, here the pseudo code:

cds_test_environment = cl_cds_test_environment=>     cds_test_environment = cl_cds_test_environment=>create_for_multiple_cds(
                               VALUE #(
                                       ( i_for_entity = 'Z_I_ENTITY_A' i_select_base_dependencies = ABAP_TRUE )
                                       ( i_for_entity = 'Z_I_ENTITY_B' i_select_base_dependencies = ABAP_TRUE )
                                     )
                             ).

DATA my_mock_data TYPE STANDARD TABLE OF Z_A_ENTITY_A.
" Fill my_mock_data with test data values
...
" Insert test data
cds_test_environment->insert_test_data( i_data = my_mock_data ).