Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
2,191
Introduction

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.

Issue

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.

Solution

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.

Example

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.

 

Note

  1. PARTIALLY IMPLEMENTED keyword,  can only be used in test classes for interfaces.

  2. 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.


Benefit

  • 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.


Conclusion

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.

 
5 Comments
Labels in this area