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.

Writing and Implementing Unit Tests 3.3

Defining Custom Evaluations of Test Results

The methods supplied inside CL_ABAP_UNIT_ASSERT are fine in 99% of cases, but

note there is an ASSERT_THAT option that lets you define your own type of test on

the result. Look at an exam ple of specialized assertion ASSERT_THAT in action.

First, create a loca l class that implements the inte rface needed when using the

ASSERT_THAT method (Listing 3.19).

CLASS lcl_my_constraint DEFINITION.

PUBLIC SECTION.

INTERFACES if_constraint.

ENDCLASS.

Listing 3.19 ASSERT_THAT

"lcl_my_constraint DEFINITION

You have to implement both methods in the interface (naturally); one performs

whatever tests you feel like on the da ta being passed in, and the other wants a

detailed message back saying what went wrong in the event of failure. In real life,

you would have a member variable to pass information from the check method to

the result method, but the example here just demonstrates the basic principle.

The example is shown in Listing 3.20.

CLASS lcl_my_constraint IMPLEMENTATION.

METHOD if_constraint~is_valid.

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

* IMPORTING data_object TYPE data

* RETURNING result TYPE abap_bool

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

* Local Variables

DATA: ld_string TYPE string.

ld_string = data_object.

result = abap_false.

CHECK ld_string CS 'SCARY'.

CHECK strlen( ld_string ) GT 5.

CHECK ld_string NS 'FLUFFY'.

result = abap_true.

ENDMETHOD.

"IF_CONSTRAINT~is_valid

159

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

Saved successfully!

Ooh no, something went wrong!