14.01.2020 Views

ABAP_to_the_Future

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Automating the Test Process 3.4

while you are going to be using a program to dynamically determine what objects

need to be created, that same program might as well also dynamically determine

what type (subclass) those created objects should be.

Listing 3.22 is an example of this approach; this code attempts to achieve the

same thing as you saw in the SETUP method in Listing 3.15, but with fewer CREATE

OBJECT statements. If you look at the definition of the SETUP method earlier in the

chapter, you’ll see three CREATE OBJECT statements, and in each case the object

you’re creating has to be defined by a DATA statement. (Some objects also need

data parameters that are elementary, my dear Watson.)

Listing 3.22 rewrites the same SETUP method, using a Z class created to use dependency

injection. Look at this like a newspaper: look at the four high level method

calls first (which represent the headlines), and then dive into the implementation

of each component method (representing the actual newspaper article) one at a

time.

First, set some values for elementary data parameters. Then, specify any subclasses

you want to substitute for real classes. Finally, create an instance of the

class under test.

DATA: mo_class_under_test TYPE REF TO ycl_monster_simulator.

zcl_bc_injector=>during_construction( :

for_parameter = 'ID_CREATOR' use_value = md_creator ),

for_parameter = 'ID_VALID_ON' use_value = sy-datum ).

"We want to use a test double for the database object

zcl_bc_injector=>instead_of(

using_main_class = 'YCL_MONSTER_PERS_LAYER'

use_sub_class = 'YCL_MOCK_MONSTER_PERS_LAYER' ).

zcl_bc_injector=>instead_of(

using_main_class = 'YCL_LOGGER’

use_sub_class = 'YCL_MOCK_LOGGER' ).

zcl_bc_injector=>create_via_injection(

CHANGING co_object = mo_class_under_test ).

Listing 3.22 SETUP Method Rewritten Using Z Statement

As you can see, when using this approach you do not have to have DATA statements

to declare the database access object or the logging object. In addition, you

only have one CREATE statement. This may not seem like much in this simple

163

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!