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

PUBLIC SECTION.

PRIVATE SECTION.

DATA: mo_class_under_test TYPE REF TO ycl_monster_simulator,

mo_mock_pers_layer TYPE REF TO ycl_mock_pers_layer,

mo_logger

TYPE REF TO ycl_mock_logger.

Listing 3.9 Defining Mock Classes to Be Injected into the Test Class

In Listing 3.6, you saw how in the constructor in production the class would create

the real classes, but during a unit test mock classes are passed into the constructor

of the class under test by the SETUP method, which runs at the start of

each test method.

The full list of the data definitions in th e test class are shown in Listing 3.10. In

addition to the mock classes, there are some global (to a class instance) variables

for things such as the input data and the result. It’s good to set things up this way

because passing parameters in and out of test methods can distract someone looking

at the code (e.g., a business expert) from making sure that the names of the

test methods reflect what is supposed to be tested.

PRIVATE SECTION.

DATA: mo_class_under_test TYPE REF TO ycl_monster_simulator,

mo_mock_pers_layer TYPE REF TO ycl_mock_pers_layer,

mo_logger

TYPE REF TO ycl_mock_logger,

ms_input_data TYPE ys_monster_input_data,

mt_bom_data

TYPE ytt_monster_bom_data,

md_creator

TYPE string.

Listing 3.10 Variables for the Test Class Definition

After defining the data, you now need to say what methods are going to be in the

test class.

Defining the SETUP Method

The first method to define is always the SETUP method, the job of which is to reset

the system state so that every test method behaves as if it were the first test

method to be run. Therefore, any of th ose evil global variables knocking about

must either be cleared or set to a certai n value, and the class under test must be

created anew. This is to avoid the so-called temporal coupling, in which the result

of one test could be influenced by the result of another test. That situation would

151

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

Saved successfully!

Ooh no, something went wrong!