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

stop my real program being dangerous, not make things worse. Therefore, I

always set this value to HARMLESS, which is what the tests I write are.)

Next is DURATION—how long you think the test will take to run. This is intended

to mirror the TIME OUT dump you get in the real system when a program goes into

an endless loop or does a full table scan on the biggest table in the database. You

can set up the expected time periods in a configuration transaction.

How long should those time periods be? Well, I’ll tell you how long I think a unit

test should take to run: it should be so fast that a human cannot even think of a

time period so small. The whole point of unit tests is that you can have a massive

amount of them and not be afraid to run the whole lot after you’ve changed even

one line of code. It’s like the syntax check: most developers run that all the time,

but they wouldn’t if it took ten minutes. Hopefully, the only reason a unit test

would take a minute or more to run is if it actually did read the database or process

a gigantic internal table in an inefficient way. If you are worried about the

method under test going into an endless loop or about having to process a really

huge internal table—sequencing a human genome or something—then you could

set the DURATION to LONG, and it would fail due to a time out. Thus far, though, I

have never found a reason to set it to anything other than SHORT.

Declaring Data Definitions

Continuing with the definition of your test class, you’ve now come to the data

declarations. The first and most important variable you declare will be a variable

to hold an instance of the class under test (a main class from the application

you’re testing various parts of).

This class will be, in accordance with good OO design principles, composed of

smaller classes that perform specific jobs, such as talking to the database. In this

example, when the application runs in real life, you would want to read the database

and output a log of the calculations for the user to see. In a unit test, you

want to do neither. Luckily, your application will be designed to use the injection

process described in Section 3.2.2 to take in a database layer and a logger as constructor

parameters, so you can pass in mock objects that will pretend to handle

interactions with the data base and logging mechanism. As you are going to be

passing in such mock objects to a constructor, you need to declare instance variables

based on those mock classes (Listing 3.9).

150

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

Saved successfully!

Ooh no, something went wrong!