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.

7

Exception Classes and Design by Contract

lo_monster ?= data_object.

result = abap_false.

CHECK lo_monster->scariness CS 'SCARY'.

CHECK lo_monster->bolts_in_neck EQ 2.

CHECK lo_monster->fluffiness EQ 0.

CHECK lo_monster->colour NE 'PINK'.

result = abap_true.

ENDMETHOD.

"IF_CONSTRAINT~is_valid

METHOD if_constraint~get_description.

*-------------------------------------------------------*

* RETURNING result TYPE string_table

*-------------------------------------------------------*

* Local Variables

DATA: ld_message TYPE string.

ld_message = 'Monster is no longer a monster!'.

APPEND ld_message TO result.

ENDMETHOD. "IF_CONSTRAINT~get_description

ENDCLASS. "Monster Constraint Implementation

Listing 7.13 Pink and Fluffy is not Scary!

You could then add the code in Listing 7.14 to the end of every method in the

monster class. In this way, you can be sure that no method call will have an

adverse effect on the essential nature of the monster.

zcl_dbc=>ensure(

that = 'The Monster is still a Monster'

which_is_true_if = lo_monster_constraint->if_constraint~is_valid( mo_

class_under_test ) ).

Listing 7.14 Calling a Class Invariant at the End of Each Method Call

Note

Instead of adding the code in Listing 7.14 to the end of every method call, you could

alternatively add such a call to the end of every unit test you do on the monster class. It

would not make much sense to do both.

280

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

Saved successfully!

Ooh no, something went wrong!