14.01.2020 Views

ABAP_to_the_Future

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Writing and Implementing Unit Tests 3.3

왘 Helper methods that call one or more ABAP statements for the sole purpose of

making the core test method read like plain English

Inside each unit test method (the methods that end with FOR TESTING), you will

have several helper methods with names that have come straight out of the specification.

As an example, the specification document says that the main purpose

of the program is to return a bill of materials (BOM) for a monster, and you do

that by having the user enter various desired monster criteria, which are then

used to calculate the BOM according to certain rules. This can be boiled down

into three sentences, and you can create helper methods with the same names as

those sentences, as shown in Listing 3.12.

given_monster_details_entered,

when_bom_is_calculated,

then_resulting_bom_is_correct,

Listing 3.12 Defining Helper Methods to Be Used in Test Methods

A method can of course do two things athe same time: hide boiler plate code and

make the remaining code look more like English. Ahead, you’ll see such an example;

you’re going to be looking up the BOM again and again in various tests, and

you do not want the reader of the test to be distracted by the mechanics of reading

from an internal table. In addition, you want the tests to read like English sentences,

so if you use a single IMPORTING parameter and a RETURNING parameter,

then you will be able to refer to BOM_LINE_ITEM( 5 ), which, although not quite

grammatically perfect, makes it clear enough to the reader what is being referred

to. The code for this is shown in Listing 3.13.

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

* Helper Methods

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

bom_line_item

IMPORTING id_line_number TYPE sy-tabix

RETURNING VALUE(rs_bom_line) LIKE LINE OF mt_bom_table.

ENDCLASS. "Test Class Definition

Listing 3.13 Helper Method to Query a BOM Line Item

3.3.2 Implementing Test Classes

Now that you’ve defined the test class, you can go ahead with the process of

implementing it. At the end of this step, you’ll be able to show the business

153

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

Saved successfully!

Ooh no, something went wrong!