Comp273 Exercises
Comp273 Exercises
DEVELOPMENT
COMP273
Exercises / Solutions
Stefan Bresch / SAP AG
Thomas Ritter / SAP AG
2
Boost the quality of your software by learning how to do test driven development in ABAP using
ABAP Unit. Then, don't just hope that you have tested the essential parts of your software, but
measure the quality of your test coverage with the ABAP Coverage Analyzer. Find all potential
bugs in your software by taking advantage of the static analysis capabilities of the code inspector.
Additionally, the session will demonstrate proven techniques for bullet-proof error handling using
advanced exception concepts and ASSERT techniques. The session also contains numerous live
demos and exercises which will make it almost impossible for you to deliver bugs in your next
software project.
Contents
TOPIC Page
Static test tools – Code Inspector Introduction to static code analysis with the Code Inspector
Dynamic tests – ABAP Unit I Introduction into writing unit tests with ABAP Unit
Dynamic tests – ABAP Unit Introduction into measuring the code coverage of unit tests by using the ABAP
Browser Unit Browser
Robust code – Exceptions I Introduction to creating and handling class based exceptions
Robust code – Exceptions II More advanced exception concepts including resumable exceptions
3
Learn how to use the Code Inspector for performing static code analysis. This includes the creation of check variants,
the creation of program variants, the creation of inspections and the execution and analysis of inspections.
Exercise description
Use the code inspector to run the extended program check or syntax
check for all our test programs.
They start all with ZTSTECHED*
Exercise solution
Learn how to create and execute local test classes. Find out how to write test methods by using the build-in assert
methods. See what happens if tests fail and discover how you can use the ABAP Unit result display to find out more
about the reasons why a particular test failed.
Exercise description
class ltcl_calculator_tests definition for testing duration short risk level harmless.
private section.
methods: divide_successful for testing.
endclass.
4. Add a method DIVIDE to the local test class which receives two integer numbers as parameters, divides them and
returns the result. The definition of the method looks like this: DIVIDE importing PARAM1 type i PARAM2 type i
returning value(result) type i.
5. Write a test which checks the result of the DIVIDE method. Use the static method
CL_AUNIT_ASSERT=>ASSERT_EQUALS() for checking whether your test is successful or not.
6. Execute your test by using the menu “Program Æ Test Æ UnitTest” or use the keyboard shortcut: Shift + Strg +
F10.
7. Get a feeling for the result display by causing your unit test to fail.
8. Once you are finished try to add more tests. What happens if you try to divide a number by zero?
5
Exercise solution
Source code
report ztbexercise2_solution.
class ltcl_calculator_tests definition for testing duration short risk level harmless.
private section.
methods: divide_successful for testing,
divide importing number1 type i
number2 type i
returning value(result) type i.
endclass. "ltcl_calculator_tests DEFINITION
method divide.
*Simplest possible implementation
result = number1 / number2.
endmethod. "divide
endclass. "ltcl_calculator_tests IMPLEMENTATION
6
Find out how to write fixtures for unit tests by using the build-in SETUP method. Use the most important assert
methods in a simple example scenario.
Exercise description
Exercise solution
Source code
report ztbexercise3_solution.
class ltcl_stack_tests definition for testing risk level harmless duration short.
private section.
methods: setup,
push_pop_successful for testing,
pop_fail for testing,
size_successful for testing.
method push_pop_successful.
data: first_value type string value 'first_value',
second_value type string value 'second_value',
act_value type string.
"Add two values to the stack
me->stack->push( first_value ).
me->stack->push( second_value ).
"Make sure that the stack returns them in the correct order
act_value = me->stack->pop( ).
cl_aunit_assert=>assert_equals( exp = second_value act = act_value ).
act_value = me->stack->pop( ).
cl_aunit_assert=>assert_equals( exp = first_value act = act_value ).
endmethod. "PUSH_POP_SUCCESSFUL
method pop_fail.
data: act_value type string.
"Call pop on an empty stack. We expect that the method returns an empty value
act_value = me->stack->pop( ).
cl_aunit_assert=>assert_initial( act_value ).
endmethod. "POP_FAIL
8
method size_successful.
"Add three items to the stack
me->stack->push( 'first' ).
me->stack->push( 'second' ).
me->stack->push( 'third' ).
"Make sure the get_size method returns the correct value
cl_aunit_assert=>assert_equals( exp = 3 act = me->stack->get_size( ) ).
endmethod. "SIZE_SUCCESSFUL
endclass. "LTCL_STACK_TESTS IMPLEMENTATION
9
Learn how you can use the ABAP Unit Browser to execute a set of unit tests and analyse the code coverage they
produce.
Exercise description
Exercise solution
1. Select the ABAP Unit Browser (If the tool is not display in the workbench add it via the menu: Utilities Æ Settings…
Æ Under tab “Workbench (General)” tick the checkbox “ABAP Unit Test Browser”).
Exercise 5 – Exceptions I
Overview
Learn how to catch exceptions by using the TRY … CATCH … ENDTRY syntax. Find out how to specify the exception
you want to catch. See how ABAP Unit handles uncaught exceptions and how to write unit tests for exception handling.
Get a better feeling how the program flow changes when you use exceptions by debugging the result of the exercise.
Exercise description
Exercise solution
Source code
report ztbexercise5_solution.
class ltcl_exceptions_tests definition for testing risk level harmless duration short.
private section.
methods: raise_exception for testing.
endclass. "ltcl_exceptions_tests DEFINITION
try.
*Divide by zero causes the exception cx_sy_zerodivide
result = 1 / 0.
*fail causes the test to fail if the exception would not occur
cl_aunit_assert=>fail( 'No exception thrown by 1 / 0!' ).
catch cx_sy_zerodivide into exception.
*The block is expected to get executed
cl_aunit_assert=>assert_bound( exception ).
catch cx_root.
*We expect the exception cx_sy-zerodivide any other exception is a bug
cl_aunit_assert=>fail( 'Unexpected exception occured!' ).
endtry.
endmethod. "raise_exception
endclass. "ltcl_exceptions_tests IMPLEMENTATION
15
Exercise 6 – Exceptions II
Overview
Learn about more advanced exception concepts by using resumable exceptions and the retry statement in a simple
example scenario. See how you can use message classes in order to define exception texts.
Exercise description
Exercise solution
Source code
report ztbexercise6_solution.
class ltcl_adv_exceptions_tests definition for testing risk level harmless duration sho
rt.
private section.
methods: retry_resume_exception for testing.
endclass. "ltcl_exceptions_tests DEFINITION
try.
*Try to add a file. The first call triggers zcx_no_space_left exception
result = filesystem->add_file( name = 'test.txt' content = 'test content' ).
*Make sure the method call was successful and the file was added
cl_aunit_assert=>assert_equals( exp = abap_true act = result ).
catch zcx_no_space_left.
*Filesystem is full. Lets clean up the recycling bin and retry
filesystem->empty_recycle_bin( ).
retry.
catch before unwind zcx_file_already_exists.
*The file already exists. We want to overwrite it and therefore resume the method call.
resume.
endtry.
endmethod. "raise_exception
endclass. "ltcl_exceptions_tests IMPLEMENTATION
17
The information in this document is proprietary to SAP. No part of this document may be reproduced, copied, or transmitted in any
form or for any purpose without the express prior written permission of SAP AG.
This document is a preliminary version and not subject to your license agreement or any other agreement with SAP. This document
contains only intended strategies, developments, and functionalities of the SAP® product and is not intended to be binding upon
SAP to any particular course of business, product strategy, and/or development. Please note that this document is subject to change
and may be changed by SAP at any time without notice.
SAP assumes no responsibility for errors or omissions in this document. SAP does not warrant the accuracy or scompleteness of the
information, text, graphics, links, or other items contained within this material. This document is provided without a warranty of any
kind, either express or implied, including but not limited to the implied warranties of merchantability, fitness for a particular purpose,
or non-infringement.
SAP shall have no liability for damages of any kind including without limitation direct, special, indirect, or consequential damages
that may result from the use of these materials. This limitation shall not apply in cases of intent or gross negligence.
The statutory liability for personal injury and defective products is not affected. SAP has no control over the information that you may
access through the use of hot links contained in these materials and does not endorse your use of third-party Web pages nor
provide any warranty whatsoever relating to third-party Web pages.