Update content
This commit is contained in:
@@ -136,7 +136,7 @@
|
||||
- are optional for all table categories.
|
||||
- can be unique/non-unique sorted keys or unique hash keys.
|
||||
- have a self-defined name. An alias name can also be specified.
|
||||
- A secondary table index is created internally for each sorted secondary key. This allows index access to hashed tables via the secondary table key is possible. In this case, `sy-tabix` is set.
|
||||
- A secondary table index is created internally for each sorted secondary key. This allows index access to hashed tables via the secondary table key. In this case, `sy-tabix` is set.
|
||||
- When accessing internal tables using the secondary table key, the key name (or the alias if specified) must be specified. They are not selected automatically. If no secondary key is specified in a processing statement, the primary key or primary table index is always used. If you want to make use of this key in ABAP statements, for example, `READ`, `LOOP AT` or `MODIFY` statements, you must specify the key explicitly using the appropriate additions, for example, `WITH ... KEY ... COMPONENTS` or `USING KEY`.
|
||||
- Use cases:
|
||||
- To improve read performance.
|
||||
@@ -1416,7 +1416,7 @@ the content are removed, but the memory space initially requested remains
|
||||
allocated. If the table is filled again later, the memory space is still
|
||||
available, which is a performance advantag over
|
||||
clearing an internal table with `FREE`. Such a statement also
|
||||
deleted the table content, but it also releases the memory
|
||||
deletes the table content, but it also releases the memory
|
||||
space.
|
||||
|
||||
``` abap
|
||||
|
||||
@@ -422,94 +422,13 @@ There are multiple ways to implement test doubles manually:
|
||||
**Injecting the test doubles**
|
||||
|
||||
As described [here](https://help.sap.com/docs/ABAP_PLATFORM_NEW/c238d694b825421f940829321ffa326a/04a2d0fc9cd940db8aedf3fa29e5f07e.html?locale=en-US), there are multiple techniques for injecting test doubles to ensure that the test doubles are used during the test run.
|
||||
To name two of them:
|
||||
- Constructor injection
|
||||
- Back door injection
|
||||
|
||||
|
||||
*Constructor injection*
|
||||
- This injection mechanism means that the test double is passed as a parameter to the instance constructor `constructor` of the class under test.
|
||||
- `constructor` declaration:
|
||||
- Has an optional importing parameter for the DOC.
|
||||
- The parameter is typed with reference to the test double, i.e. an object of the test double is passed.
|
||||
- `constructor` implementation:
|
||||
- In the example beloew, it is assumed that an interface exists for the DOC, and a test double has been implemented that implements that interface.
|
||||
- A reference variable with a type reference to the interface is declared in the declaration part of the class under test.
|
||||
- When the unit test is executed, an object of the test double is created in the test method. This object is then passed to the `constructor`. A check is implemented to determine if the reference variable is bound. During the unit test execution, it is indeed bound, and the test double is injected. If the unit test is not run and the class is executed, an object of the *regular data provider* (e.g. a class that implements the interface for production use) is created. Therefore, it is ensured that the test double is only used in the context of a unit test run.
|
||||
|
||||
Such a constructor injection might look like this.
|
||||
|
||||
``` abap
|
||||
"See the executable example for the complete picture.
|
||||
"The code in this snippet refers to the production code.
|
||||
...
|
||||
"Class declaration part
|
||||
DATA ref_data_provider TYPE REF TO if_data,
|
||||
|
||||
"Optional parameter for passing an object of the test touble
|
||||
METHODS constructor
|
||||
IMPORTING iref_data_prov TYPE REF TO tld_test_double OPTIONAL.
|
||||
|
||||
....
|
||||
|
||||
"Class implementation part
|
||||
METHOD constructor.
|
||||
"If the object of the test touble is not passed, an object is created for the actual data provider
|
||||
|
||||
IF iref_data_prov IS BOUND.
|
||||
|
||||
"Note: The parameter is only bound when running in ABAP unit test
|
||||
ref_data_provider = iref_data_prov.
|
||||
Among them, there are the following. They are demonstrated in the executable example. Check the code and comments in the [global class](./src/zcl_demo_abap_unit_test.clas.abap) and [test include](./src/zcl_demo_abap_unit_test.clas.testclasses.abap) of the example.
|
||||
- Constructor injection: The test double is passed as a parameter to the instance constructor `constructor` of the class under test.
|
||||
- Setter injection: The test double is passed as a parameter to a setter method.
|
||||
- Parameter injection: The test double is passed as a parameter to the tested method (i.e. an optional importing parameter) in the class under test.
|
||||
- Back door injection: A *back door* is created to inject a test double into the class under test. This *back door* is implemented by granting [friendship](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenfriend_glosry.htm) to the test class. This makes internal attributes of the class under test accessible from the test class.
|
||||
|
||||
ELSE.
|
||||
|
||||
ref_data_provider = NEW cl_data_provider( ).
|
||||
|
||||
ENDIF.
|
||||
ENDMETHOD.
|
||||
```
|
||||
|
||||
|
||||
*Back door injection*
|
||||
- This injection mechanism means that a *back door* is created to inject a test double into the class under test.
|
||||
- This *back door* is implemented by granting [friendship](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenfriend_glosry.htm) to the test class. This makes internal attributes of the class under test accessible from the test class.
|
||||
- The back door injection enters the picture when internal attributes of the class under test are changed during the test run. That is, in the production code, you have declared a reference variable with a type reference to the data provider in the private section, for example.
|
||||
- Note: Similar to above, in the example below, it is assumed that there is an interface for the DOC and that a test double has been implemented that implements that interface.
|
||||
- When the unit test is executed, the private attribute of the class under test is changed, and an object of the test double is injected.
|
||||
|
||||
|
||||
``` abap
|
||||
"See the executable example for the complete picture.
|
||||
"The code in this snippet refers to the test class.
|
||||
|
||||
"Test class declaration part
|
||||
...
|
||||
|
||||
"class under test
|
||||
DATA ref_cut TYPE REF TO cl_class_under_test.
|
||||
|
||||
"if_data_provider: interface with which local test data are created
|
||||
"by implementing an interface method
|
||||
DATA ref_data_prov TYPE REF TO if_data_provider.
|
||||
|
||||
...
|
||||
|
||||
"Implementation of the setup method in the test class
|
||||
METHOD setup.
|
||||
|
||||
ref_cut = NEW #( ).
|
||||
|
||||
"Assumption: The local test double ltd_test_double implements the interface if_data_provider
|
||||
ref_data_prov = new ltd_test_double( ).
|
||||
|
||||
"Back door injection
|
||||
"The reference variable declared in the class under test is assigned an object
|
||||
"of the test double. In the class under test, the reference variable is defined
|
||||
"with type TYPE REF TO if_data_provider.
|
||||
ref_cut->ref_var_defined_in_cut = ref_data_prov.
|
||||
ENDMETHOD.
|
||||
```
|
||||
|
||||
<p align="right">(<a href="#top">back to top</a>)</p>
|
||||
|
||||
### Test Seams
|
||||
|
||||
@@ -54,483 +54,613 @@
|
||||
"! Example to demonstrate ABAP unit tests.<br>Choose F9 in ADT to run the class.
|
||||
"! To run all unit tests of the class, choose Ctrl/Cmd + Shift + F10.
|
||||
CLASS zcl_demo_abap_unit_test DEFINITION
|
||||
PUBLIC
|
||||
CREATE PUBLIC.
|
||||
PUBLIC
|
||||
CREATE PUBLIC.
|
||||
|
||||
PUBLIC SECTION.
|
||||
PUBLIC SECTION.
|
||||
|
||||
INTERFACES: if_oo_adt_classrun.
|
||||
INTERFACES: if_oo_adt_classrun.
|
||||
|
||||
CLASS-METHODS: class_constructor.
|
||||
CLASS-METHODS: class_constructor.
|
||||
|
||||
"Optional parameter for the instance constructor for the purpose of
|
||||
"constructor injection
|
||||
METHODS constructor
|
||||
IMPORTING iref_data_prov TYPE REF TO zdemo_abap_get_data_itf OPTIONAL.
|
||||
"Optional parameter for the instance constructor for the purpose of
|
||||
"constructor injection
|
||||
METHODS constructor
|
||||
IMPORTING iref_data_prov TYPE REF TO zdemo_abap_get_data_itf OPTIONAL.
|
||||
|
||||
PROTECTED SECTION.
|
||||
PROTECTED SECTION.
|
||||
|
||||
TYPES carr_tab TYPE TABLE OF zdemo_abap_fli WITH EMPTY KEY.
|
||||
TYPES carr_tab TYPE TABLE OF zdemo_abap_fli WITH EMPTY KEY.
|
||||
|
||||
METHODS: select_flight_data IMPORTING carrier TYPE zdemo_abap_fli-carrid
|
||||
RETURNING VALUE(flight_data) TYPE carr_tab.
|
||||
METHODS: select_flight_data IMPORTING carrier TYPE zdemo_abap_fli-carrid
|
||||
RETURNING VALUE(flight_data) TYPE carr_tab.
|
||||
|
||||
PRIVATE SECTION.
|
||||
PRIVATE SECTION.
|
||||
|
||||
TYPES: int_tab_so TYPE SORTED TABLE OF i WITH UNIQUE KEY table_line,
|
||||
int_tab_st TYPE STANDARD TABLE OF i WITH EMPTY KEY,
|
||||
occ_rate TYPE p LENGTH 4 DECIMALS 2,
|
||||
TYPES: int_tab_so TYPE SORTED TABLE OF i WITH UNIQUE KEY table_line,
|
||||
int_tab_st TYPE STANDARD TABLE OF i WITH EMPTY KEY,
|
||||
occ_rate TYPE p LENGTH 4 DECIMALS 2,
|
||||
|
||||
BEGIN OF nums_struc,
|
||||
num1 TYPE i,
|
||||
num2 TYPE i,
|
||||
END OF nums_struc,
|
||||
BEGIN OF nums_struc,
|
||||
num1 TYPE i,
|
||||
num2 TYPE i,
|
||||
END OF nums_struc,
|
||||
|
||||
nums_tab TYPE TABLE OF nums_struc WITH EMPTY KEY.
|
||||
nums_tab TYPE TABLE OF nums_struc WITH EMPTY KEY.
|
||||
|
||||
DATA: seats_table TYPE zdemo_abap_get_data_itf=>carr_tab,
|
||||
flight_tab TYPE TABLE OF zdemo_abap_fli,
|
||||
DATA: seats_table TYPE zdemo_abap_get_data_itf=>carr_tab,
|
||||
flight_tab TYPE TABLE OF zdemo_abap_fli,
|
||||
|
||||
"Reference variable for back door injection
|
||||
"Note: In the example, it is a local interface declared in the
|
||||
"Local Types tab (the CCIMP include). To make the type known to
|
||||
"the global class, see the Class-relevant Local Types tab (the CCDEF include).
|
||||
data_provider_local_itf TYPE REF TO lif_get_data,
|
||||
"Reference variable for back door injection
|
||||
"Note: In the example, it is a local interface declared in the
|
||||
"Local Types tab (the CCIMP include). To make the type known to
|
||||
"the global class, see the Class-relevant Local Types tab (the CCDEF include).
|
||||
data_provider_local_itf TYPE REF TO lif_get_data,
|
||||
|
||||
"Reference variable for constructor injection
|
||||
"In the example, the type refers to a global interface.
|
||||
data_provider_global_itf TYPE REF TO zdemo_abap_get_data_itf.
|
||||
"Reference variable for constructor injection
|
||||
"In the example, the type refers to a global interface.
|
||||
data_provider_global_itf TYPE REF TO zdemo_abap_get_data_itf,
|
||||
|
||||
METHODS:
|
||||
"Calculates the sum of two numbers
|
||||
"This method demonstrates the use of the setup and teardown methods in the test class.
|
||||
get_sum IMPORTING key TYPE zdemo_abap_tab1-key_field
|
||||
char TYPE zdemo_abap_tab1-char1
|
||||
RETURNING VALUE(sum) TYPE i,
|
||||
"For demonstrating setter injection
|
||||
data_provider_setter_inj TYPE REF TO zdemo_abap_get_data_itf,
|
||||
|
||||
"Calculates the common divisors and the greatest common divisor of two numbers
|
||||
get_common_div_and_gcd IMPORTING a TYPE i
|
||||
b TYPE i
|
||||
EXPORTING common_divisors TYPE int_tab_so
|
||||
gcd TYPE i,
|
||||
"For demonstrating parameter injection
|
||||
data_provider_param_inj TYPE REF TO zdemo_abap_get_data_itf..
|
||||
|
||||
"Calculates the digit sum of a number
|
||||
get_digit_sum IMPORTING num TYPE i
|
||||
RETURNING VALUE(digit_sum) TYPE i,
|
||||
METHODS:
|
||||
"Calculates the sum of two numbers
|
||||
"This method demonstrates the use of the setup and teardown methods in the test class.
|
||||
get_sum IMPORTING key TYPE zdemo_abap_tab1-key_field
|
||||
char TYPE zdemo_abap_tab1-char1
|
||||
RETURNING VALUE(sum) TYPE i,
|
||||
|
||||
"Multiple methods that all do the same (they calculate the occupancy rate of flights)
|
||||
"but serve different demonstration purposes for the ABAP unit tests in the example.
|
||||
"The method implementations involve a depended-on component (DOC). In this case,
|
||||
"it is a database access.
|
||||
"Calculates the common divisors and the greatest common divisor of two numbers
|
||||
get_common_div_and_gcd IMPORTING a TYPE i
|
||||
b TYPE i
|
||||
EXPORTING common_divisors TYPE int_tab_so
|
||||
gcd TYPE i,
|
||||
|
||||
"Method to demonstrate test double injection using inheritance and method redefinition
|
||||
get_occ_rate_using_meth IMPORTING carrier_id TYPE zdemo_abap_fli-carrid
|
||||
RETURNING VALUE(occupancy_rate) TYPE occ_rate,
|
||||
"Calculates the digit sum of a number
|
||||
get_digit_sum IMPORTING num TYPE i
|
||||
RETURNING VALUE(digit_sum) TYPE i,
|
||||
|
||||
"Method to demonstrate test double injection using test seams
|
||||
get_occ_rate_test_seam IMPORTING carrier_id TYPE zdemo_abap_fli-carrid
|
||||
EXPORTING occupancy_rate TYPE occ_rate
|
||||
num1 TYPE i
|
||||
num2 TYPE i,
|
||||
"Multiple methods that all do the same (they calculate the occupancy rate of flights)
|
||||
"but serve different demonstration purposes for the ABAP unit tests in the example.
|
||||
"The method implementations involve a depended-on component (DOC). In this case,
|
||||
"it is a database access.
|
||||
"The methods are intentionally implemented in a similar way. Therefore, almost
|
||||
"all of the following methods will display the same output in the console when the
|
||||
"class is executed using F9.
|
||||
|
||||
"Method to demonstrate test double injection using back door injection and a local interface
|
||||
get_occ_rate_local_itf IMPORTING carrier_id TYPE zdemo_abap_fli-carrid
|
||||
RETURNING VALUE(occupancy_rate) TYPE occ_rate,
|
||||
"Method to demonstrate test double injection using inheritance and method redefinition
|
||||
get_occ_rate_using_meth IMPORTING carrier_id TYPE zdemo_abap_fli-carrid
|
||||
RETURNING VALUE(occupancy_rate) TYPE occ_rate,
|
||||
|
||||
"Method to demonstrate test double injection using constructor injection and a global interface
|
||||
get_occ_rate_global_itf IMPORTING carrier_id TYPE zdemo_abap_fli-carrid
|
||||
RETURNING VALUE(occupancy_rate) TYPE occ_rate.
|
||||
"Method to demonstrate test double injection using test seams
|
||||
get_occ_rate_test_seam IMPORTING carrier_id TYPE zdemo_abap_fli-carrid
|
||||
EXPORTING occupancy_rate TYPE occ_rate
|
||||
num1 TYPE i
|
||||
num2 TYPE i,
|
||||
|
||||
"Method to demonstrate test double injection using back door injection and a local interface
|
||||
get_occ_rate_local_itf IMPORTING carrier_id TYPE zdemo_abap_fli-carrid
|
||||
RETURNING VALUE(occupancy_rate) TYPE occ_rate,
|
||||
|
||||
"Method to demonstrate test double injection using constructor injection and a global interface
|
||||
get_occ_rate_global_itf IMPORTING carrier_id TYPE zdemo_abap_fli-carrid
|
||||
RETURNING VALUE(occupancy_rate) TYPE occ_rate,
|
||||
|
||||
"Method to demonstrate test double injection using setter injection and a global interface
|
||||
get_occ_rate_setter_inj IMPORTING carrier_id TYPE zdemo_abap_fli-carrid
|
||||
RETURNING VALUE(occupancy_rate) TYPE occ_rate,
|
||||
|
||||
"Method for setter injection
|
||||
setter_meth IMPORTING data_prov TYPE REF TO zdemo_abap_get_data_itf,
|
||||
|
||||
"Method to demonstrate test double injection using parameter injection and a global interface
|
||||
"An optional parameter is specified for passing the test double if the method is tested.
|
||||
get_occ_rate_param_inj IMPORTING carrier_id TYPE zdemo_abap_fli-carrid
|
||||
data_prov TYPE REF TO zdemo_abap_get_data_itf OPTIONAL
|
||||
RETURNING VALUE(occupancy_rate) TYPE occ_rate.
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
CLASS zcl_demo_abap_unit_test IMPLEMENTATION.
|
||||
|
||||
METHOD if_oo_adt_classrun~main.
|
||||
"Note: The example includes a couple of implementations for the methods
|
||||
"declared above. And by choosing F9 in ADT, you can run the class and check the
|
||||
"output in the console.
|
||||
"However, the focus of the example is unit tests. Therefore, check the
|
||||
"test classes and methods in the test include (Test Classes tab in ADT).
|
||||
"Choose Ctrl/Cmd + Shift + F10 to launch all tests in the class and check the
|
||||
"test results in the ABAP Unit tab in ADT.
|
||||
METHOD if_oo_adt_classrun~main.
|
||||
"Note: The example includes a couple of implementations for the methods
|
||||
"declared above. And by choosing F9 in ADT, you can run the class and check the
|
||||
"output in the console.
|
||||
"However, the focus of the example is unit tests. Therefore, check the
|
||||
"test classes and methods in the test include (Test Classes tab in ADT).
|
||||
"Choose Ctrl/Cmd + Shift + F10 to launch all tests in the class and check the
|
||||
"test results in the ABAP Unit tab in ADT.
|
||||
|
||||
DATA(output) = NEW zcl_demo_abap_display( out ).
|
||||
DATA(output) = NEW zcl_demo_abap_display( out ).
|
||||
|
||||
output->display( `ABAP Cheat Sheet Demonstration Example: A Glimpse on ABAP Unit Tests` ).
|
||||
output->display( `1) get_sum Method` ).
|
||||
"This method demonstrates the use of the setup and teardown methods in the test class.
|
||||
output->display( `ABAP Cheat Sheet Demonstration Example: A Glimpse on ABAP Unit Tests` ).
|
||||
output->display( `1) get_sum Method` ).
|
||||
"This method demonstrates the use of the setup and teardown methods in the test class.
|
||||
|
||||
DATA(sum) = get_sum( key = 1 char = 'aaa' ).
|
||||
DATA(sum) = get_sum( key = 1 char = 'aaa' ).
|
||||
|
||||
output->display( input = sum name = `sum` ).
|
||||
output->display( input = sum name = `sum` ).
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `2) get_common_div_and_gcd Method` ).
|
||||
output->next_section( `2) get_common_div_and_gcd Method` ).
|
||||
|
||||
"Filling an internal table with numbers on whose bases the common divisors and the
|
||||
"greatest common divisor are to be calculated
|
||||
DATA(tab) = VALUE nums_tab( ( num1 = 10 num2 = 20 )
|
||||
( num1 = 100 num2 = 200 )
|
||||
( num1 = 12 num2 = 6 )
|
||||
( num1 = 5 num2 = 1 )
|
||||
( num1 = 50 num2 = 50 )
|
||||
( num1 = 4 num2 = 8 ) ).
|
||||
"Filling an internal table with numbers on whose bases the common divisors and the
|
||||
"greatest common divisor are to be calculated
|
||||
DATA(tab) = VALUE nums_tab( ( num1 = 10 num2 = 20 )
|
||||
( num1 = 100 num2 = 200 )
|
||||
( num1 = 12 num2 = 6 )
|
||||
( num1 = 5 num2 = 1 )
|
||||
( num1 = 50 num2 = 50 )
|
||||
( num1 = 4 num2 = 8 ) ).
|
||||
|
||||
LOOP AT tab ASSIGNING FIELD-SYMBOL(<a>).
|
||||
LOOP AT tab ASSIGNING FIELD-SYMBOL(<a>).
|
||||
|
||||
get_common_div_and_gcd( EXPORTING a = <a>-num1
|
||||
b = <a>-num2
|
||||
IMPORTING common_divisors = DATA(common_divs) gcd = DATA(gcd) ).
|
||||
get_common_div_and_gcd( EXPORTING a = <a>-num1
|
||||
b = <a>-num2
|
||||
IMPORTING common_divisors = DATA(common_divs) gcd = DATA(gcd) ).
|
||||
|
||||
output->display( |Common divisors of { <a>-num1 } and { <a>-num2 }| ).
|
||||
output->display( |Common divisors of { <a>-num1 } and { <a>-num2 }| ).
|
||||
|
||||
output->display( input = common_divs name = `common_divs` ).
|
||||
output->display( input = common_divs name = `common_divs` ).
|
||||
|
||||
output->display( |Greatest common divisor of { <a>-num1 } and { <a>-num2 }: { gcd } | ).
|
||||
output->display( |Greatest common divisor of { <a>-num1 } and { <a>-num2 }: { gcd } | ).
|
||||
|
||||
ENDLOOP.
|
||||
ENDLOOP.
|
||||
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `3) get_digit_sum Method` ).
|
||||
output->next_section( `3) get_digit_sum Method` ).
|
||||
|
||||
"Filling an internal table with numbers on whose bases the digit sum is to be calculated
|
||||
DATA(tab_i) = VALUE int_tab_so( ( 12 )
|
||||
( 123 )
|
||||
( 3 )
|
||||
( 8246 )
|
||||
( 1001001 )
|
||||
( 0 ) ).
|
||||
"Filling an internal table with numbers on whose bases the digit sum is to be calculated
|
||||
DATA(tab_i) = VALUE int_tab_so( ( 12 )
|
||||
( 123 )
|
||||
( 3 )
|
||||
( 8246 )
|
||||
( 1001001 )
|
||||
( 0 ) ).
|
||||
|
||||
LOOP AT tab_i ASSIGNING FIELD-SYMBOL(<b>).
|
||||
LOOP AT tab_i ASSIGNING FIELD-SYMBOL(<b>).
|
||||
|
||||
DATA(digit_sum) = get_digit_sum( <b> ).
|
||||
DATA(digit_sum) = get_digit_sum( <b> ).
|
||||
|
||||
output->display( |The digit sum of { <b> } is { digit_sum }.| ).
|
||||
output->display( |The digit sum of { <b> } is { digit_sum }.| ).
|
||||
|
||||
ENDLOOP.
|
||||
ENDLOOP.
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `4) get_occ_rate_using_meth Method` ).
|
||||
"In the test class, this method demonstrates test double injection
|
||||
"using inheritance and method redefinition.
|
||||
output->next_section( `4) get_occ_rate_using_meth Method` ).
|
||||
"In the test class, this method demonstrates test double injection
|
||||
"using inheritance and method redefinition.
|
||||
|
||||
"Filling an internal table with carrier ids on whose bases the occupancy
|
||||
"rate is to be calculated.
|
||||
DATA(tab_str) = VALUE zdemo_abap_get_data_itf=>carr_tab( ( carrid = 'LH' )
|
||||
( carrid = 'AA' )
|
||||
( carrid = 'DL' ) ).
|
||||
"Filling an internal table with carrier ids on whose bases the occupancy
|
||||
"rate is to be calculated.
|
||||
DATA(tab_str) = VALUE zdemo_abap_get_data_itf=>carr_tab( ( carrid = 'LH' )
|
||||
( carrid = 'AA' )
|
||||
( carrid = 'DL' ) ).
|
||||
|
||||
LOOP AT tab_str ASSIGNING FIELD-SYMBOL(<c>).
|
||||
LOOP AT tab_str ASSIGNING FIELD-SYMBOL(<c>).
|
||||
|
||||
DATA(occupancy_rate) = get_occ_rate_using_meth( <c>-carrid ).
|
||||
DATA(occupancy_rate) = get_occ_rate_using_meth( <c>-carrid ).
|
||||
|
||||
output->display( |The occupancy rate for airline { <c>-carrid } is { occupancy_rate }%.| ).
|
||||
output->display( |The occupancy rate for airline { <c>-carrid } is { occupancy_rate }%.| ).
|
||||
|
||||
ENDLOOP.
|
||||
output->next_section( `5) get_occ_rate_test_seam Method` ).
|
||||
"This method demonstrates test double injection using test seams.
|
||||
ENDLOOP.
|
||||
output->next_section( `5) get_occ_rate_test_seam Method` ).
|
||||
"This method demonstrates test double injection using test seams.
|
||||
|
||||
LOOP AT tab_str ASSIGNING FIELD-SYMBOL(<d>).
|
||||
LOOP AT tab_str ASSIGNING FIELD-SYMBOL(<d>).
|
||||
|
||||
get_occ_rate_test_seam( EXPORTING carrier_id = <d>-carrid
|
||||
IMPORTING occupancy_rate = DATA(occupancy_rate_test_seam)
|
||||
num1 = DATA(num1)
|
||||
num2 = DATA(num2) ).
|
||||
get_occ_rate_test_seam( EXPORTING carrier_id = <d>-carrid
|
||||
IMPORTING occupancy_rate = DATA(occupancy_rate_test_seam)
|
||||
num1 = DATA(num1)
|
||||
num2 = DATA(num2) ).
|
||||
|
||||
output->display( |The occupancy rate for airline { <d>-carrid } is { occupancy_rate_test_seam }%.| ).
|
||||
output->display( |The occupancy rate for airline { <d>-carrid } is { occupancy_rate_test_seam }%.| ).
|
||||
|
||||
output->display( |num1: { num1 }| ).
|
||||
output->display( |num2: { num2 }| ).
|
||||
output->display( |num1: { num1 }| ).
|
||||
output->display( |num2: { num2 }| ).
|
||||
|
||||
ENDLOOP.
|
||||
ENDLOOP.
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `6) get_occ_rate_local_itf Method` ).
|
||||
"This method demonstrates test double injection using back door
|
||||
"injection and a local interface.
|
||||
output->next_section( `6) get_occ_rate_local_itf Method` ).
|
||||
"This method demonstrates test double injection using back door
|
||||
"injection and a local interface.
|
||||
|
||||
LOOP AT tab_str ASSIGNING FIELD-SYMBOL(<e>).
|
||||
LOOP AT tab_str ASSIGNING FIELD-SYMBOL(<e>).
|
||||
|
||||
DATA(occupancy_rate_local_itf) = get_occ_rate_local_itf( <e>-carrid ).
|
||||
DATA(occupancy_rate_local_itf) = get_occ_rate_local_itf( <e>-carrid ).
|
||||
|
||||
output->display( |The occupancy rate for airline { <e>-carrid } is { occupancy_rate_local_itf }%.| ).
|
||||
output->display( |The occupancy rate for airline { <e>-carrid } is { occupancy_rate_local_itf }%.| ).
|
||||
|
||||
ENDLOOP.
|
||||
ENDLOOP.
|
||||
|
||||
**********************************************************************
|
||||
|
||||
output->next_section( `7) get_occ_rate_global_itf Method` ).
|
||||
"This method demonstrates test double injection using constructor
|
||||
"injection and a global interface.
|
||||
output->next_section( `7) get_occ_rate_global_itf Method` ).
|
||||
"This method demonstrates test double injection using constructor
|
||||
"injection and a global interface.
|
||||
|
||||
LOOP AT tab_str ASSIGNING FIELD-SYMBOL(<f>).
|
||||
LOOP AT tab_str ASSIGNING FIELD-SYMBOL(<f>).
|
||||
|
||||
DATA(occupancy_rate_global_itf) = get_occ_rate_global_itf( <f>-carrid ).
|
||||
DATA(occupancy_rate_global_itf) = get_occ_rate_global_itf( <f>-carrid ).
|
||||
|
||||
output->display( |The occupancy rate for airline { <f>-carrid } is { occupancy_rate_global_itf }%.| ).
|
||||
output->display( |The occupancy rate for airline { <f>-carrid } is { occupancy_rate_global_itf }%.| ).
|
||||
|
||||
ENDLOOP.
|
||||
ENDLOOP.
|
||||
|
||||
ENDMETHOD.
|
||||
**********************************************************************
|
||||
|
||||
METHOD class_constructor.
|
||||
"Filling demo database tables.
|
||||
zcl_demo_abap_flight_tables=>fill_dbtabs( ).
|
||||
output->next_section( `8) get_occ_rate_setter_inj Method` ).
|
||||
"This method demonstrates test double injection using setter injection and a global interface.
|
||||
|
||||
"Preparing a demo database table for this example (get_sum method)
|
||||
DELETE FROM zdemo_abap_tab1.
|
||||
INSERT zdemo_abap_tab1 FROM @(
|
||||
VALUE #( key_field = 1 char1 = 'aaa' char2 = 'bbb' num1 = 25 num2 = 75 ) ).
|
||||
ENDMETHOD.
|
||||
LOOP AT tab_str ASSIGNING FIELD-SYMBOL(<g>).
|
||||
|
||||
METHOD constructor.
|
||||
DATA(occupancy_rate_setter_inj) = get_occ_rate_setter_inj( <g>-carrid ).
|
||||
|
||||
"For demonstrating the back door injection
|
||||
data_provider_local_itf = NEW lcl_data_prov_local_itf( ).
|
||||
output->display( |The occupancy rate for airline { <g>-carrid } is { occupancy_rate_setter_inj }%.| ).
|
||||
|
||||
"For demonstrating the constructor injection
|
||||
IF iref_data_prov IS BOUND.
|
||||
"Note: The parameter is only bound when you run the unit test.
|
||||
"When you run the unit test and you debug, you will see that iref_data_prov
|
||||
"has a type reference to LTD_TEST_DATA_GLOBAL_INTF.
|
||||
ENDLOOP.
|
||||
|
||||
data_provider_global_itf = iref_data_prov.
|
||||
**********************************************************************
|
||||
|
||||
ELSE.
|
||||
output->next_section( `9) get_occ_rate_param_inj Method` ).
|
||||
"This method demonstrates test double injection using parameter injection and a global interface.
|
||||
|
||||
data_provider_global_itf = NEW lcl_data_prov_glo_itf( ).
|
||||
LOOP AT tab_str ASSIGNING FIELD-SYMBOL(<h>).
|
||||
|
||||
DATA(occupancy_rate_param_inj) = get_occ_rate_param_inj( carrier_id = <h>-carrid ).
|
||||
|
||||
output->display( |The occupancy rate for airline { <h>-carrid } is { occupancy_rate_param_inj }%.| ).
|
||||
|
||||
ENDLOOP.
|
||||
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD class_constructor.
|
||||
"Filling demo database tables.
|
||||
zcl_demo_abap_flight_tables=>fill_dbtabs( ).
|
||||
|
||||
"Preparing a demo database table for this example (get_sum method)
|
||||
DELETE FROM zdemo_abap_tab1.
|
||||
INSERT zdemo_abap_tab1 FROM @(
|
||||
VALUE #( key_field = 1 char1 = 'aaa' char2 = 'bbb' num1 = 25 num2 = 75 ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD constructor.
|
||||
|
||||
"For demonstrating the back door injection
|
||||
data_provider_local_itf = NEW lcl_data_prov_local_itf( ).
|
||||
|
||||
"For demonstrating the constructor injection
|
||||
IF iref_data_prov IS BOUND.
|
||||
"Note: The parameter is only bound when you run the unit test.
|
||||
"When you run the unit test and you debug, you will see that iref_data_prov
|
||||
"has a type reference to LTD_TEST_DATA_GLOBAL_INTF.
|
||||
|
||||
data_provider_global_itf = iref_data_prov.
|
||||
|
||||
ELSE.
|
||||
|
||||
data_provider_global_itf = NEW lcl_data_prov_glo_itf( ).
|
||||
|
||||
ENDIF.
|
||||
|
||||
"Object creation for the method call in the get_occ_rate_setter_inj method
|
||||
data_provider_setter_inj = NEW lcl_data_prov_glo_itf( ).
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_sum.
|
||||
"The method selects a record from a database table and sums the values
|
||||
"of two fields, both are of type i.
|
||||
|
||||
SELECT SINGLE
|
||||
FROM zdemo_abap_tab1
|
||||
FIELDS num1 + num2 AS sum
|
||||
WHERE key_field = @key
|
||||
AND char1 = @char
|
||||
INTO @sum.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_common_div_and_gcd.
|
||||
"Calculates the common divisors and the greatest common divisor of two numbers
|
||||
|
||||
CLEAR: common_divisors, gcd.
|
||||
|
||||
CHECK a >= 1.
|
||||
CHECK b >= 1.
|
||||
|
||||
IF a >= b.
|
||||
DATA(greater_num) = a.
|
||||
DATA(lower_num) = b.
|
||||
ELSE.
|
||||
greater_num = b.
|
||||
lower_num = a.
|
||||
ENDIF.
|
||||
|
||||
"Getting common divisors
|
||||
DATA(div) = 1.
|
||||
|
||||
WHILE div <= lower_num.
|
||||
IF lower_num MOD div = 0.
|
||||
DATA(divisor) = lower_num / div.
|
||||
INSERT divisor INTO TABLE common_divisors.
|
||||
ENDIF.
|
||||
|
||||
ENDMETHOD.
|
||||
div += 1.
|
||||
ENDWHILE.
|
||||
|
||||
METHOD get_sum.
|
||||
"The method selects a record from a database table and sums the values
|
||||
"of two fields, both are of type i.
|
||||
LOOP AT common_divisors ASSIGNING FIELD-SYMBOL(<i>).
|
||||
|
||||
SELECT SINGLE
|
||||
FROM zdemo_abap_tab1
|
||||
FIELDS num1 + num2 AS sum
|
||||
WHERE key_field = @key
|
||||
AND char1 = @char
|
||||
INTO @sum.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_common_div_and_gcd.
|
||||
"Calculates the common divisors and the greatest common divisor of two numbers
|
||||
|
||||
CLEAR: common_divisors, gcd.
|
||||
|
||||
CHECK a >= 1.
|
||||
CHECK b >= 1.
|
||||
|
||||
IF a >= b.
|
||||
DATA(greater_num) = a.
|
||||
DATA(lower_num) = b.
|
||||
ELSE.
|
||||
greater_num = b.
|
||||
lower_num = a.
|
||||
IF greater_num MOD <i> <> 0.
|
||||
DELETE common_divisors WHERE table_line = <i>.
|
||||
ENDIF.
|
||||
|
||||
"Getting common divisors
|
||||
DATA(div) = 1.
|
||||
ENDLOOP.
|
||||
|
||||
WHILE div <= lower_num.
|
||||
IF lower_num MOD div = 0.
|
||||
DATA(divisor) = lower_num / div.
|
||||
INSERT divisor INTO TABLE common_divisors.
|
||||
ENDIF.
|
||||
"Extracting the greatest common divisor from the list of common divisors
|
||||
gcd = common_divisors[ lines( common_divisors ) ].
|
||||
|
||||
div += 1.
|
||||
ENDWHILE.
|
||||
ENDMETHOD.
|
||||
|
||||
LOOP AT common_divisors ASSIGNING FIELD-SYMBOL(<g>).
|
||||
METHOD get_digit_sum.
|
||||
"Calculates the digit sum of a number
|
||||
|
||||
IF greater_num MOD <g> <> 0.
|
||||
DELETE common_divisors WHERE table_line = <g>.
|
||||
ENDIF.
|
||||
CLEAR digit_sum.
|
||||
|
||||
ENDLOOP.
|
||||
CHECK num >= 0.
|
||||
|
||||
"Extracting the greatest common divisor from the list of common divisors
|
||||
gcd = common_divisors[ lines( common_divisors ) ].
|
||||
DATA(converted_int) = CONV string( num ).
|
||||
DATA(len) = strlen( converted_int ).
|
||||
|
||||
ENDMETHOD.
|
||||
DO len TIMES.
|
||||
DATA(idx) = sy-index - 1.
|
||||
digit_sum = digit_sum + converted_int+idx(1).
|
||||
ENDDO.
|
||||
|
||||
METHOD get_digit_sum.
|
||||
"Calculates the digit sum of a number
|
||||
ENDMETHOD.
|
||||
|
||||
CLEAR digit_sum.
|
||||
|
||||
CHECK num >= 0.
|
||||
|
||||
DATA(converted_int) = CONV string( num ).
|
||||
DATA(len) = strlen( converted_int ).
|
||||
|
||||
DO len TIMES.
|
||||
DATA(idx) = sy-index - 1.
|
||||
digit_sum = digit_sum + converted_int+idx(1).
|
||||
ENDDO.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_occ_rate_test_seam.
|
||||
"Method to demonstrate test double injection using test seams
|
||||
"Note: The code is just for demonstration purposes. Of course, the result can be
|
||||
"achieved more elegantly using SQL expressions, for example.
|
||||
|
||||
TEST-SEAM select_flights.
|
||||
"DOC
|
||||
SELECT seatsmax, seatsocc
|
||||
FROM zdemo_abap_fli
|
||||
WHERE carrid = @carrier_id
|
||||
INTO CORRESPONDING FIELDS OF TABLE @seats_table.
|
||||
END-TEST-SEAM.
|
||||
|
||||
DATA total_seatsmax_tm TYPE i.
|
||||
DATA total_seatsocc_tm TYPE i.
|
||||
|
||||
LOOP AT seats_table ASSIGNING FIELD-SYMBOL(<h>).
|
||||
|
||||
total_seatsmax_tm = total_seatsmax_tm + <h>-seatsmax.
|
||||
total_seatsocc_tm = total_seatsocc_tm + <h>-seatsocc.
|
||||
|
||||
ENDLOOP.
|
||||
|
||||
occupancy_rate = total_seatsocc_tm / total_seatsmax_tm * 100.
|
||||
|
||||
"Further examples for test seams
|
||||
DATA(var) = 0.
|
||||
|
||||
"Empty test seam; code is injected during unit test
|
||||
"Check the output when running the class using F9 and
|
||||
"the test results when running the unit test.
|
||||
TEST-SEAM num1.
|
||||
END-TEST-SEAM.
|
||||
|
||||
IF var = 0.
|
||||
num1 = 1.
|
||||
ELSE.
|
||||
num1 = 999.
|
||||
ENDIF.
|
||||
|
||||
num2 = 0.
|
||||
|
||||
"Empty injection
|
||||
"See the test class: The code that is included in the test
|
||||
"seam should be excluded from the test. Therefore, the
|
||||
"test injection block in the test class is empty.
|
||||
"Check the output when running the class using F9 and
|
||||
"the test results when running the unit test.
|
||||
TEST-SEAM num2.
|
||||
num2 = 123.
|
||||
END-TEST-SEAM.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_occ_rate_local_itf.
|
||||
"Method to demonstrate test double injection using back door
|
||||
"injection and a local interface
|
||||
|
||||
DATA total_seatsmax_local_itf TYPE i.
|
||||
DATA total_seatsocc_local_itf TYPE i.
|
||||
|
||||
"Assumption: The original code in this method was as follows (the line commented out).
|
||||
"It was identified as DOC (reading data from a database table).
|
||||
|
||||
"DATA(flight_data) = select_flight_data( carrier = carrier_id ).
|
||||
|
||||
"Instead of a method call like above and for a proper unit testing - a global interface
|
||||
"is not available - a local interface is created, and
|
||||
"an interface method is implemented. In this example, the local interface is
|
||||
"created in the local types tab (CCIMP include): lif_get_data
|
||||
"A local class (lcl_data_prov_local_itf) is also created in the CCIMP include. It
|
||||
"implements the local interface.
|
||||
|
||||
"When running the class with F9, the object used here refers to type lcl_data_prov_local_itf.
|
||||
"When running the unit test, the object used here refers to type ltd_test_data_local_itf,
|
||||
"i.e. the local test double is injected.
|
||||
DATA(flight_data) = data_provider_local_itf->select_flight_data( carrier = carrier_id ).
|
||||
|
||||
LOOP AT flight_data ASSIGNING FIELD-SYMBOL(<i>).
|
||||
|
||||
total_seatsmax_local_itf = total_seatsmax_local_itf + <i>-seatsmax.
|
||||
total_seatsocc_local_itf = total_seatsocc_local_itf + <i>-seatsocc.
|
||||
|
||||
ENDLOOP.
|
||||
|
||||
occupancy_rate = total_seatsocc_local_itf / total_seatsmax_local_itf * 100.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_occ_rate_global_itf.
|
||||
"Method to demonstrate test double injection using constructor injection
|
||||
"and a global interface
|
||||
|
||||
DATA total_seatsmax_global_itf TYPE i.
|
||||
DATA total_seatsocc_global_itf TYPE i.
|
||||
|
||||
"Assumption: The original code in this method was as follows (the line commented out).
|
||||
"It was identified as DOC (reading data from a database table).
|
||||
|
||||
"DATA(flight_data) = select_flight_data( carrier = carrier_id ).
|
||||
|
||||
"Instead of a method call like above and for a proper unit testing, a global interface
|
||||
"is provided.
|
||||
"In the example, an interface method is implemented in a local class in the local types
|
||||
"tab (CCIMP include): lcl_data_prov_glo_itf
|
||||
|
||||
"When running the class with F9, the object used here refers to type lcl_data_prov_glo_itf.
|
||||
"When running the unit test, the object used here refers to type ltd_test_data_global_intf,
|
||||
"i.e. the local test double is injected.
|
||||
|
||||
DATA(flight_data) = data_provider_global_itf->select_flight_data( carrier = carrier_id ).
|
||||
|
||||
LOOP AT flight_data ASSIGNING FIELD-SYMBOL(<j>).
|
||||
|
||||
total_seatsmax_global_itf = total_seatsmax_global_itf + <j>-seatsmax.
|
||||
total_seatsocc_global_itf = total_seatsocc_global_itf + <j>-seatsocc.
|
||||
|
||||
ENDLOOP.
|
||||
|
||||
occupancy_rate = total_seatsocc_global_itf / total_seatsmax_global_itf * 100.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD select_flight_data.
|
||||
"Method that is identified as DOC in the method implementations above.
|
||||
"This method is also used for demonstrating test double injection and method redefinition.
|
||||
METHOD get_occ_rate_test_seam.
|
||||
"Method to demonstrate test double injection using test seams
|
||||
"Note: The code is just for demonstration purposes. Of course, the result can be
|
||||
"achieved more elegantly using SQL expressions, for example.
|
||||
|
||||
TEST-SEAM select_flights.
|
||||
"DOC
|
||||
SELECT seatsmax, seatsocc
|
||||
FROM zdemo_abap_fli
|
||||
WHERE carrid = @carrier
|
||||
INTO CORRESPONDING FIELDS OF TABLE @flight_data.
|
||||
ENDMETHOD.
|
||||
WHERE carrid = @carrier_id
|
||||
INTO CORRESPONDING FIELDS OF TABLE @seats_table.
|
||||
END-TEST-SEAM.
|
||||
|
||||
METHOD get_occ_rate_using_meth.
|
||||
"This method demonstrates test double injection using inheritance and method redefinition.
|
||||
DATA total_seatsmax_tm TYPE i.
|
||||
DATA total_seatsocc_tm TYPE i.
|
||||
|
||||
DATA total_seatsmax_no TYPE i.
|
||||
DATA total_seatsocc_no TYPE i.
|
||||
LOOP AT seats_table ASSIGNING FIELD-SYMBOL(<j>).
|
||||
|
||||
"During the unit test, the redefined method in the test class is called.
|
||||
DATA(flight_data) = select_flight_data( carrier = carrier_id ).
|
||||
total_seatsmax_tm = total_seatsmax_tm + <j>-seatsmax.
|
||||
total_seatsocc_tm = total_seatsocc_tm + <j>-seatsocc.
|
||||
|
||||
LOOP AT flight_data ASSIGNING FIELD-SYMBOL(<k>).
|
||||
ENDLOOP.
|
||||
|
||||
total_seatsmax_no = total_seatsmax_no + <k>-seatsmax.
|
||||
total_seatsocc_no = total_seatsocc_no + <k>-seatsocc.
|
||||
occupancy_rate = total_seatsocc_tm / total_seatsmax_tm * 100.
|
||||
|
||||
ENDLOOP.
|
||||
"Further examples for test seams
|
||||
DATA(var) = 0.
|
||||
|
||||
occupancy_rate = total_seatsocc_no / total_seatsmax_no * 100.
|
||||
"Empty test seam; code is injected during unit test
|
||||
"Check the output when running the class using F9 and
|
||||
"the test results when running the unit test.
|
||||
TEST-SEAM num1.
|
||||
END-TEST-SEAM.
|
||||
|
||||
ENDMETHOD.
|
||||
IF var = 0.
|
||||
num1 = 1.
|
||||
ELSE.
|
||||
num1 = 999.
|
||||
ENDIF.
|
||||
|
||||
ENDCLASS.
|
||||
num2 = 0.
|
||||
|
||||
"Empty injection
|
||||
"See the test class: The code that is included in the test
|
||||
"seam should be excluded from the test. Therefore, the
|
||||
"test injection block in the test class is empty.
|
||||
"Check the output when running the class using F9 and
|
||||
"the test results when running the unit test.
|
||||
TEST-SEAM num2.
|
||||
num2 = 123.
|
||||
END-TEST-SEAM.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_occ_rate_local_itf.
|
||||
"Method to demonstrate test double injection using back door
|
||||
"injection and a local interface
|
||||
|
||||
DATA total_seatsmax_local_itf TYPE i.
|
||||
DATA total_seatsocc_local_itf TYPE i.
|
||||
|
||||
"Assumption: The original code in this method was as follows (the line commented out).
|
||||
"It was identified as DOC (reading data from a database table).
|
||||
|
||||
"DATA(flight_data) = select_flight_data( carrier = carrier_id ).
|
||||
|
||||
"Instead of a method call like above and for a proper unit testing - a global interface
|
||||
"is not available - a local interface is created, and
|
||||
"an interface method is implemented. In this example, the local interface is
|
||||
"created in the local types tab (CCIMP include): lif_get_data
|
||||
"A local class (lcl_data_prov_local_itf) is also created in the CCIMP include. It
|
||||
"implements the local interface.
|
||||
|
||||
"When the class is executed using F9, the object used here refers to type lcl_data_prov_local_itf.
|
||||
"When the unit test is executed, the object used here refers to type ltd_test_data_local_itf,
|
||||
"i.e. the local test double is injected.
|
||||
DATA(flight_data) = data_provider_local_itf->select_flight_data( carrier = carrier_id ).
|
||||
|
||||
LOOP AT flight_data ASSIGNING FIELD-SYMBOL(<k>).
|
||||
|
||||
total_seatsmax_local_itf = total_seatsmax_local_itf + <k>-seatsmax.
|
||||
total_seatsocc_local_itf = total_seatsocc_local_itf + <k>-seatsocc.
|
||||
|
||||
ENDLOOP.
|
||||
|
||||
occupancy_rate = total_seatsocc_local_itf / total_seatsmax_local_itf * 100.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_occ_rate_global_itf.
|
||||
"Method to demonstrate test double injection using constructor injection
|
||||
"and a global interface
|
||||
|
||||
DATA total_seatsmax_global_itf TYPE i.
|
||||
DATA total_seatsocc_global_itf TYPE i.
|
||||
|
||||
"Assumption: The original code in this method was as follows (the line commented out).
|
||||
"It was identified as DOC (reading data from a database table).
|
||||
|
||||
"DATA(flight_data) = select_flight_data( carrier = carrier_id ).
|
||||
|
||||
"Instead of a method call like above and for a proper unit testing, a global interface
|
||||
"is provided.
|
||||
"In the example, an interface method is implemented in a local class in the local types
|
||||
"tab (CCIMP include): lcl_data_prov_glo_itf
|
||||
|
||||
"When the class is executed using F9, the object used here refers to type lcl_data_prov_glo_itf.
|
||||
"When the unit test is executed, the object used here refers to type ltd_test_data_global_intf,
|
||||
"i.e. the local test double is injected.
|
||||
DATA(flight_data) = data_provider_global_itf->select_flight_data( carrier = carrier_id ).
|
||||
|
||||
LOOP AT flight_data ASSIGNING FIELD-SYMBOL(<l>).
|
||||
|
||||
total_seatsmax_global_itf = total_seatsmax_global_itf + <l>-seatsmax.
|
||||
total_seatsocc_global_itf = total_seatsocc_global_itf + <l>-seatsocc.
|
||||
|
||||
ENDLOOP.
|
||||
|
||||
occupancy_rate = total_seatsocc_global_itf / total_seatsmax_global_itf * 100.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD select_flight_data.
|
||||
"Method that is identified as DOC in the method implementations above.
|
||||
"This method is also used for demonstrating test double injection and method redefinition.
|
||||
|
||||
SELECT seatsmax, seatsocc
|
||||
FROM zdemo_abap_fli
|
||||
WHERE carrid = @carrier
|
||||
INTO CORRESPONDING FIELDS OF TABLE @flight_data.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_occ_rate_using_meth.
|
||||
"This method demonstrates test double injection using inheritance and method redefinition.
|
||||
|
||||
DATA total_seatsmax_no TYPE i.
|
||||
DATA total_seatsocc_no TYPE i.
|
||||
|
||||
"During the unit test, the redefined method in the test class is called.
|
||||
DATA(flight_data) = select_flight_data( carrier = carrier_id ).
|
||||
|
||||
LOOP AT flight_data ASSIGNING FIELD-SYMBOL(<m>).
|
||||
|
||||
total_seatsmax_no = total_seatsmax_no + <m>-seatsmax.
|
||||
total_seatsocc_no = total_seatsocc_no + <m>-seatsocc.
|
||||
|
||||
ENDLOOP.
|
||||
|
||||
occupancy_rate = total_seatsocc_no / total_seatsmax_no * 100.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_occ_rate_setter_inj.
|
||||
"This method demonstrates test double injection using setting injection.
|
||||
"See the setter_meth method.
|
||||
|
||||
DATA total_seatsmax_setter_inj TYPE i.
|
||||
DATA total_seatsocc_setter_inj TYPE i.
|
||||
|
||||
"Assumption: The original code in this method was as follows (the line commented out).
|
||||
"It was identified as DOC (reading data from a database table).
|
||||
|
||||
"DATA(flight_data) = select_flight_data( carrier = carrier_id ).
|
||||
|
||||
"Instead of a method call like above and for a proper unit testing, a global interface
|
||||
"is provided.
|
||||
"In the example, an interface method is implemented in a local class in the local types
|
||||
"tab (CCIMP include): lcl_data_prov_glo_itf
|
||||
|
||||
"See the comment in the setter_meth method
|
||||
DATA(flight_data) = data_provider_setter_inj->select_flight_data( carrier = carrier_id ).
|
||||
|
||||
LOOP AT flight_data ASSIGNING FIELD-SYMBOL(<n>).
|
||||
|
||||
total_seatsmax_setter_inj = total_seatsmax_setter_inj + <n>-seatsmax.
|
||||
total_seatsocc_setter_inj = total_seatsocc_setter_inj + <n>-seatsocc.
|
||||
|
||||
ENDLOOP.
|
||||
|
||||
occupancy_rate = total_seatsocc_setter_inj / total_seatsmax_setter_inj * 100.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD setter_meth.
|
||||
"Method to demonstrate the test double injection using setter injection
|
||||
|
||||
"When the unit test is executed, an object of the test double class is passed as
|
||||
"a parameter. Then, the object used here refers to type ltd_test_data_setter_inj,
|
||||
"i.e. the local test double is injected.
|
||||
data_provider_setter_inj = data_prov.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD get_occ_rate_param_inj.
|
||||
"This method demonstrates test double injection using parameter injection.
|
||||
|
||||
DATA total_seatsmax_param_inj TYPE i.
|
||||
DATA total_seatsocc_param_inj TYPE i.
|
||||
|
||||
"Assumption: The original code in this method was as follows (the line commented out).
|
||||
"It was identified as DOC (reading data from a database table).
|
||||
|
||||
"DATA(flight_data) = select_flight_data( carrier = carrier_id ).
|
||||
|
||||
"Instead of a method call like above and for a proper unit testing, a global interface
|
||||
"is provided.
|
||||
"In the example, an interface method is implemented in a local class in the local types
|
||||
"tab (CCIMP include): lcl_data_prov_glo_itf
|
||||
|
||||
"The method has an optional importing parameter. When the unit test is executed,
|
||||
"the parameter is bound. An object of the test double class is passed in that case.
|
||||
"Otherwise, when the class is executed using F9, an object of the actual data provider
|
||||
"is created.
|
||||
IF data_prov IS BOUND.
|
||||
data_provider_param_inj = data_prov.
|
||||
ELSE.
|
||||
data_provider_param_inj = NEW lcl_data_prov_glo_itf( ).
|
||||
ENDIF.
|
||||
|
||||
DATA(flight_data) = data_provider_param_inj->select_flight_data( carrier = carrier_id ).
|
||||
|
||||
LOOP AT flight_data ASSIGNING FIELD-SYMBOL(<o>).
|
||||
|
||||
total_seatsmax_param_inj = total_seatsmax_param_inj + <o>-seatsmax.
|
||||
total_seatsocc_param_inj = total_seatsocc_param_inj + <o>-seatsocc.
|
||||
|
||||
ENDLOOP.
|
||||
|
||||
occupancy_rate = total_seatsocc_param_inj / total_seatsmax_param_inj * 100.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user