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.

3

ABAP Unit and Test-Driven Development

These variables are then set up during

shown in Listing 3.5.

construction of the object instance, as

METHOD constructor.

CREATE OBJECT mo_logger.

CREATE OBJECT mo_pers_layer

EXPORTING

io_logger = mo_logger " Logging Class

id_valid_on = sy-datum. " Validaty Date

ENDMETHOD.

"constructor

Listing 3.5 Variables Set Up During Construction of Object Instance

However, as you can see, this design does not include any mock objects, which

means that you have no chance to run unit tests against the class. To solve this

problem, you need a way to get the mock objects you created earlier inside the

class under test. The best time to do this is when an instance of the class under

test is being created.

When creating an instance of the class under test, you use a technique called constructor

injection to make the code use the mock objects so that it behaves differently

than it would when running produc tively. With this technique, you still

have private instance variables of the da tabase access classes (for example), but

now you make these into optional import parameters of the constructor. The constructor

definition and implementation now looks like the code in Listing 3.6.

PUBLIC SECTION.

METHODS: constructor IMPORTING

io_pers_layer TYPE REF TO ycl_monster_pers_layer OPTIONAL

io_logger TYPE REF TO ycl_logger OPTIONAL.

METHOD constructor."Implementation

IF io_logger IS SUPPLIED.

mo_logger = io_logger.

ELSE.

CREATE OBJECT mo_logger.

ENDIF.

IF io_pers_layer IS SUPPLIED.

mo_pers_layer = io_pers_layer.

146

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

Saved successfully!

Ooh no, something went wrong!