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.

3

ABAP Unit and Test-Driven Development

Step Three: Calling the Production Code to Be Tested

The time has come to actually invoke th e code to be tested: you are calling precisely

one method or other type of routine, you pass in your hard-coded dummy

values, you call the routine, and you usua lly get some sort of result back. The

important thing is that the routine being called does not know it is being called

from a test method; the business logic behaves exactly as it would in production,

with the exception that when it interacts with the database or another external

system it is really dealing with mock classes.

In this example, when you pass in the hard-coded input data, the real business logic

will be executed, and a list of monster components is passed back (Listing 3.17).

"WHEN..........................

METHOD when_bom_is_calculated.

mo_class_under_test->simulate_bom(

EXPORTING is_bom_input_data = ms_input_data

IMPORTING et_bom = mt_bom_data ).

ENDMETHOD."when_bom_is_calculated

Listing 3.17 Calling the Production Code to Be Tested

The method that calls the code to be tested should be very short—for example, a

call to a single function module or a method—for two reasons:

왘 Clarity

Anyone reading the test code should be able to tell exactly what the input data

is, what routine processes this data, and what form the result data comes back

in. Calling several methods in a row distracts someone reading the code and

makes them have to spend extra time working out what is going on.

왘 Ease of maintenance

You want to hide the implementation de tails of what is being tested from the

test method; this way, even if those implementation details change, the test

method does not need to change.

For example, in a procedural prog ram, you may call two or three PERFORM statements

in a row when it would be better to do a call to a single FORM routine—so

that if you were to add another FORM routine in the real program, you wouldn’t

have to go and add it to the test method as well. With procedural programs, it

would be good if you had a signature with the input and output values, but a lot

156

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

Saved successfully!

Ooh no, something went wrong!