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.

3

ABAP Unit and Test-Driven Development

Mock Objects vs. Stub Objects

When talking about mock objects, the terms stub and mock are often used interchangeably;

technically, though, there is a differen ce. If you are testing how your class affects

an external system, then the fake external system is a mock, and if you are testing how

the fake external system affects your class, then the fake external system is a stub.

(Either way, the point is that you use a fake external system for testing.)

3.2.1 Creating Mock Objects

For testing purposes, you want to define mock classes and mock objects. Mock

classes are classes that run in the development environment. They don’t really try

to read and write to the database, send emails, fire nuclear missiles, and so on, but

they test the business logic nonetheless. Mock objects follow the same principles

as regular objects; i.e., in the same way that a monster object is an instance of the

real monster class, a mock monster object is an instance of a mock monster class.

This is where the basic features of object-oriented programming come into play:

subclasses and interfaces. To continue the previous example, you’ll next create a

subclass of the database access class that doesn’t actually read the database but

instead redefines the database access methods to return hard-coded values based

upon the values passed in. In Listing 3. 3, you’ll see some possible redefined

implementations of methods in mock subclasses that could replace the real classes

in the example.

METHOD read_customising. "mock database implementation

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

* IMPORTING input_value

* EXPORTING export_vlaue

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

CASE input_value.

WHEN one_value.

export_value = something.

WHEN another_value.

export_value = something_else.

WHEN OTHERS.

export_value = something_else_again.

ENDCASE.

ENDMETHOD. "read customising mock database implementation

METHOD popup_to_confirm. "mock user interface implementation

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

* RETURNING rd_answer TYPE char01

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

144

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

Saved successfully!

Ooh no, something went wrong!