Show TOC

Using Constraints in ABAP Unit TestingLocate this document in the navigation structure

Use

Unit testing with constraints verifies that data objects changed or produced by tested modules meet specified conditions.

You can test many constraints with ready-made methods of the CL_ABAP_UNIT_ASSERT class.

For example, you can use CL_ABAP_UNIT_ASSERT methods to verify that the result of a tested module is a value between 1 and 10. Or you can verify that the result is a bound reference to an ABAP class.

However, in some unit tests you may need to apply multiple constraints in a logical relationship to a data object. Or you may need to apply your own application-specific constraint to a data object.

You can add custom constraings to unit testing by doing the following:

  • Implementing a new constraint: You can express new constraints that are not already implemented in CL_ABAP_UNIT_ASSERT by creating and using a class that implements IF_CONSTRAINT.

  • Applying multiple constraints in a logical relationship: You can use CL_AUNIT_CONSTRAINTS in your test method to apply logical operations to testing of constraints. For example, you can specify that one of two constraints in an XOR (exclusive or) relationship must apply to a tested data object.

You can test a single constraint (an instance of an IF_CONSTRAINT class) or multiple constraints (bound together by CL_AUNIT_CONSTRAINTS) against a data object with the ASSERT_THAT method of CL_ABAP_UNIT_ASSERT.

The EXP parameter of ASSERT_THAT expects an instance of a class that implements IF_CONSTRAINT.

Applying Multiple Constraints

You can test for multiple constraints in a logical relationship by doing the following:

  1. Specify the set of constraints with classes that implement the IF_CONSTRAINT interface.

    Evaluate whether the tested data object satisfies the constraint in the IS_VALID method.

    Return a description of the constraint in the GET_DESCRIPTION method.

  2. Create instances of the constraints classes in your ABAP Unit test method.

  3. Pass the instances of the IF_CONSTRAINT classes to the appropriate static method of CL_AUNIT_CONSTRAINTS in your test method. Possible methods are as follows:

    • AND: All of up to four constraints (as expressed by instances of I F_CONSTRAINTS classes, in their IS_VALID methods) must be met for the test to succeed.

    • NAND: At least one and up to three of four possible constraints must be met for the test to succeed.

    • NOR: None of up to four possible constraints may be met for the test to succeed.

    • NOT: The specified single constraint must not be met for the test to succeed.

    • OR: Any of up to four constraints must be met for the test to succeed.

    • XOR: One of two constraints must be met for the test to succeed.

  4. Pass the IF_CONSTRAINT object returned in Step 3 to CL_ABAP_UNIT_ASSERT=>ASSERT_THAT as the argument for the EXP parameter. EXP expresses the constraints that the tested data object in the ACT parameter must fulfill.

    The ASSERT_THAT method uses the IF_CONSTRAINT~ IS_VALID methods of the instances to test whether a data object meets the conditions set by the constraints.

    Depending on the result of the test, the ASSERT_THAT method reports a unit test failure or not.

    Should you wish to test a single constraint, you can skip step 3 above and pass your IF_CONSTRAINT instance directly to the ASSERT_THAT method. The ASSERT_THAT method will use the IS_VALID method in the instance to check whether the constraint is fulfilled.

    DATA:
    
      constraint_1 TYPE REF TO cl_implements_if_constraint,
      constraint_2 TYPE REF TO cl_implements_if_constraint,
      compound_constraint TYPE REF TO if_constraint.
    
    CREATE OBJECT constraint_1 
      EXPORTING condition_1 = <value_to_test>
    
    CREATE OBJECT constraint_2
      EXPORTING condition_1 = <value_to_test>
    
    ... < call to method under test >
    
    compound_constraint = cl_aunit_constraint=>and( 
     c1 = constraint_1       
     c2 = constraint_2 ).
    
    cl_abap_unit_assert=>assert_that(
      act = < data object to check >
      exp = compound_constraint ).
    
                      

Implementing Your Own Constraint

To implement your own IF_CONSTRAINT class, you must do the following:

  • Expose any parameters, such as required integer or character values, in the constructor of your class. The constructor should save the values as attributes of your class.

  • Test whether a data object fulfills your constraint(s) in the IF_CONSTRAINT~IS_VALID method. The data object is provided to this method as an input parameter.

  • Provide a message text describing the test result in the unit test with the IF_CONSTRAINT~GET_DESCRIPTION method.