Interface injection is one of the core concepts, used to remove ABAP dependency in unit test class. When Interface Injection is used in Unit Test Class then we need to implement ALL the available methods. This was causing the low coverage of class and also resulting ATC(ABAP Test Cockpit) error.
If ALL the available interface methods are not implemented, then syntax warning occurs in the test class causing ATC error in next systems.
The warning is occurring because of not implementing INF1 interface add method.
Using PARTIALLY IMPLEMENTED keyword, we can only implement those methods which are required for our UTC. This will prevent the syntax warning, if not all the interface methods are implemented in the unit test class. This keyword should be used while defining a interface as shown below.
Use the syntax
INTERFACES <interface_name> PARTIALLY IMPLEMENTED.
The ltc_help_demo is the local class created in UTC to remove the dependency of the object. In the below example, Class ltc_help_demo implements the interface INF1, which is having more than 10 methods. Only 2 methods (add and sub) are required in this unit test class to execute a test scenario.
Local Class Definition:
CLASS ltc_help_demo DEFINITION FOR TESTING. PUBLIC SECTION. INTERFACES: <INF1> PARTIALLY IMPLEMENTED. ENDCLASS.
Now Implement only required methods.
CLASS ltc_help_demo IMPLEMENTATION. METHOD INF1~add. ENDMETHOD. METHOD INF1~sub. ENDMETHOD. ENDCLASS.
- PARTIALLY IMPLEMENTED keyword, can only be used in test classes for interfaces.
- If an interface method, which is not implemented, is called during a test execution, an exception is raised from the class CX_SY_DYN_CALL_ILLEGAL_METHOD.
- It will reduce ATC error (in Development and Testing Systems) significantly.
- It will increase lines no. of coverage.
- It will increase coverage for number of methods.
Using PARTIALLY IMPLEMENTED, we can implement only required interface methods for the test. This is useful when a class implements an interface and not all the interface methods are used in the code being tested.