21.03.2013 Views

Object Oriented ABAP

Object Oriented ABAP

Object Oriented ABAP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Actual result – 24.<br />

Expected Result – 25.<br />

Tolerance – 0.9999.<br />

Difference = Expected Result - Actual result.<br />

= 1 > tolerance.<br />

Therefore displays an error.<br />

Example Program:<br />

Let us consider an example for <strong>ABAP</strong> unit test using the method ASSERT_EQUALS to check<br />

the equality of two data objects. In this program, we have two methods divide and factorial<br />

in a local class MATH. We want to test the factorial method. So we have created one class<br />

and one method MYTEST for testing. In the test method implementation we have called the<br />

factorial method and so the data object RESULT is populated. Now we are going to compare<br />

the actual data object (RESULT) with the expected result. For that we are calling the<br />

ASSERT_EQUALS from the global class passing the expected result.<br />

*----------------------------------------------------------------------*<br />

* CLASS math DEFINITION<br />

*----------------------------------------------------------------------*<br />

*<br />

*----------------------------------------------------------------------*<br />

CLASS math DEFINITION.<br />

PUBLIC SECTION.<br />

METHODS divide<br />

IMPORTING opr1 TYPE i<br />

opr2 TYPE i<br />

EXPORTING result TYPE f<br />

RAISING cx_sy_arithmetic_error.<br />

METHODS factorial<br />

IMPORTING n TYPE i<br />

RETURNING value(fact) TYPE i.<br />

ENDCLASS. "math DEFINITION<br />

*----------------------------------------------------------------------*<br />

* CLASS math IMPLEMENTATION<br />

*----------------------------------------------------------------------*<br />

*<br />

*----------------------------------------------------------------------*<br />

CLASS math IMPLEMENTATION.<br />

METHOD divide.<br />

result = opr2 / opr1.<br />

ENDMETHOD. "divide<br />

METHOD factorial.<br />

fact = 1.<br />

IF n = 0.<br />

RETURN.<br />

ELSE.<br />

DO n TIMES.<br />

fact = fact * sy-index.<br />

ENDDO.

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

Saved successfully!

Ooh no, something went wrong!