Show TOC

Procedure documentationManaging Dependencies in ABAP Unit Testing Locate this document in the navigation structure

Procedure

Breaking dependencies on external code and resources is one of the keys to successful test-driven development. If the code under test contains too many dependencies not secured by their own trustworthy testing, then the unit test becomes meaningless. Or at least, it is no longer a test of the discrete unit of code under test.

At present, the strategy for managing dependencies in ABAP is as follows:

  • You need to ensure that ABAP objects used for method calls can be replaced with test doubles when ABAP Unit tests are running. That is, the dependencies must be 'injectable', so that the production object can be swapped out for a test object when testing.

  • You need to encapsulate non-OO dependencies such as the following in local or global ABAP classes:

    • Database operations

    • Calls to function modules and executable programs

    • Interaction with user interfaces

    • Authorization checks (the ABAP AUTHORITY-CHECK statement)

    • Use of files, system fields, and other resources.

    For example, your code should not use the database or call a function module directly, but only by way of an encapsulating ABAP object. The object uses the database or calls a function for you.

    You need to ensure that these ABAP objects as well are 'injectable', so that the dependencies encapsulated by these objects can be broken for testing.

You can use ABAP Units setup and teardown methods to set up and clear away any test fixture that you need. For example, you can create test objects for injection into the code under test.

Patterns for Injecting Dependencies

Here are some patterns for ways to break dependencies by making dependencies injectable.

Constructor Dependency Injection for ABAP Unit Testing

Private Dependency Injection for ABAP Unit Testing