cd265 exercise

27
5/23/2018 CD265Exercise-slidepdf.com http://slidepdf.com/reader/full/cd265-exercise 1/27  ABAP TEST AND TROUBLESHOOTING TOOLS  –  INCLUDING NEW ABAP TEST COCKPIT CD265 Exercises / Solutions Gebhardt Boris, Ekaterina Zavozina / SAP AG

Upload: tamanam-kiran-kumar

Post on 13-Oct-2015

6 views

Category:

Documents


0 download

DESCRIPTION

CD265 EXERCISE

TRANSCRIPT

  • ABAP TEST AND TROUBLESHOOTING TOOLS

    INCLUDING NEW ABAP TEST COCKPIT

    CD265

    Exercises / Solutions Gebhardt Boris, Ekaterina Zavozina / SAP AG

  • 2

    EXERCISE 1 ABAP TEST COCKPIT FOR DEVELOPERS

    Exercise description

    Find errors in the report ZCD265_E1_ using ABAP Test Cockpit (ATC). Which ATC findings are the most dangerous?

    Correct the ATC findings

    Use Check Again functionality to make sure youve corrected all the ATC findings in the report ZCD265_E1_

  • 3

    Exercise solution

    1. Open transaction SE80, display the report ZCD265_E1_ 2. Via context menu choose Check -> ABAP Test Cockpit you will see five findings:

    The most dangerous are the following findings:

    Formal Parameter Incompatible with Actual Parameter Function Module Parameter Missing

    They will definitely result in a runtime abortion in the production system.

    3. By double clicking on a finding you will receive a detailed description of it:

    4. Correct, for example, BREAK-POINT Statement finding by simply removing the statement from the code. To navigate to the finding from the ATC result click on a link with object name or on Display Object link in the details to finding:

  • 4

    Correct the finding and go back to the ATC result. Press the Check Again button:

    For every corrected finding youll see a green light:

  • 5

    EXERCISE 2 ABAP TEST COCKPIT: WORKING WITH ACTIVE RESULT

    Exercise description

    Part 1: Developer apply for an exemption

    1. Look at the active central ATC result in the ATC Result Browser. For how many ATC

    findings are you contact person? 2. Adjust the current filter to display findings with priority 1. Try out other filter criteria 3. Apply for an exemption for the ATC finding in the report ZCD265_E2_

    Part 2: Quality expert

    1. Open the active central ATC result 2. Switch to Advanced Filter. Find out using advanced filter how many ATC findings were

    reported for package ZTEST_WALLET 3. Display the aggregated view of the ATC findings. How many priority 1 ATC findings of

    Extended Program Check does the active ATC result contain? 4. Reset default filter for displaying ATC central results. New filter should display priority 1 and

    priority 2 findings of package ZTEST_CD265_EXERCISES_ every time you double click on a central ATC result in the ATC Result Browser

  • 6

    Exercise solution part 1

    1. Look at the active central ATC result in the ATC Result Browser. For how many ATC findings are you contact person?

    a. Open transaction SE80, select the ATC Result Browser (if the tool is not displayed in the SE80, add it via the menu: Utilities Settings Tab Workbench (General) tick the checkbox ATC Result Browser)

    b. Double click on the active ATC result. It is marked with a yellow bulb c. You will see all ATC findings relevant to your user. This is a default value of the filter

    for central results

    2. Adjust the current filter to display findings with priority 1. Try out other filter criteria a. Click on + button:

    b. And choose Priority from dropdown box with different filter criteria:

    c. Enter 1 in the input field and hit Enter or press the Apply Filter button

    3. Apply for an exemption for the ATC finding in the report ZCD265_E2_ a. Adjust filter to display all ATC findings for your user b. Open details to priority 2 ATC finding Analysis of WHERE Condition c. Click on Apply for an Exemption:

  • 7

    d. Granularity and scope of the exemption can be adjusted here. Press Continue:

    e. On the next page enter approver, reason for the exemption and some text in the Justification field. Press Complete:

    Exercise solution part 2

    1. Open the active central ATC result 2. Switch to Advanced Filter. Find out using advanced filter how many ATC findings were

    reported for package ZTEST_WALLET a. Press Switch Filter Type and choose option Advanced Filter:

  • 8

    b. Adjust filter values: Delete your user name and enter ZTEST_WALLET to the Package select option, press Ok:

    3. Display the aggregated view of the ATC findings. How many priority 1 ATC findings of Extended Program Check does the active ATC result contain?

    a. Click on Statistics View button to display the aggregated view of the ATC findings:

    b. Click on Clear Filter to display all ATC findings:

    c. Now you can see how many priority 1 findings were reported by Extended Program Check:

  • 9

    4. Reset default filter for displaying ATC central results. New filter should display priority 1 and

    priority 2 findings of package ZTEST_CD265_EXERCISES_ every time you double click on central ATC results in the ATC Result Browser.

    a. Switch to Advanced filter and enter package ZTEST_CD265_EXERCISES_ and priority 1, 2 as filter criteria

    b. Save filter variant:

    c. Go to Utilities Settings Tab ABAP Test Cockpit, choose option Filter Variant and enter the name of your filter variant:

    d. Restart transaction SE80 and double click on an ATC central result

  • 10

    EXERCISE 3 WRITE ABAP UNIT TEST

    Exercise description

    Report ZCD265_E3_ contains a local class LCL_SIMPLE_CALCULATOR. It supports four operations: Add, subtract, multiply, divide.

    1. Write a unit test DIVIDE_SUCCESSFUL for method DIVIDE of the local class LCL_SIMPLE_CALCULATOR

    2. Add more unit tests. What happens if you try to divide a number by zero? Dont forget to correct the production code!

  • 11

    Exercise solution part 1

    CLASS ltc_calculator DEFINITION FOR TESTING DURATION

    SHORT RISK LEVEL HARMLESS.

    PRIVATE SECTION.

    METHODS: divide_successful FOR TESTING.

    ENDCLASS.

    CLASS ltc_calculator IMPLEMENTATION.

    METHOD divide_successful.

    DATA:

    calculator TYPE REF TO lcl_simple_calculator,

    actual_result TYPE decfloat16.

    CREATE OBJECT calculator.

    actual_result = calculator->divide( i_param1 = 12 i_param2 = 4 ).

    cl_aunit_assert=>assert_equals( exp = 3 act = actual_result ).

    ENDMETHOD.

    ENDCLASS.

    Exercise solution part 2

    Define new unit test method DIVISION_BY_ZERO:

    CLASS ltc_calculator DEFINITION FOR TESTING DURATION

    SHORT RISK LEVEL HARMLESS.

    PRIVATE SECTION.

    METHODS: divide_successful FOR TESTING.

    METHODS: division_by_zero for TESTING.

    ENDCLASS.

    Implement test method:

    METHOD division_by_zero.

    DATA:

    calculator TYPE REF TO lcl_simple_calculator.

    CREATE OBJECT calculator.

    TRY.

    calculator->divide( i_param1 = 12 i_param2 = 0 ).

    cl_aunit_assert=>fail( 'Expected exception lcx_invalid_parameter has not been

    raised' ).

    CATCH lcx_invalid_parameter.

    " Success

    ENDTRY.

    ENDMETHOD.

    Introduce new local exception class: CLASS lcx_invalid_parameter DEFINITION INHERITING FROM cx_dynamic_check.

    ENDCLASS.

  • 12

    Activate the report and execute unit tests. You will see that the new unit test fails. As next step adjust your production code. Definition section: METHODS divide

    IMPORTING

    i_param1 TYPE i

    i_param2 TYPE i

    RETURNING VALUE(r_result) TYPE decfloat16

    RAISING lcx_invalid_parameter.

    Implementation: METHOD divide.

    IF ( i_param2 = 0 ).

    RAISE EXCEPTION TYPE lcx_invalid_parameter.

    ENDIF.

    r_result = i_param1 / i_param2.

    ENDMETHOD.

    Activate it and execute unit tests again. They are green now. And dont forget to adjust the method RUN: METHOD run.

    ...

    CASE abap_true.

    WHEN p_add.

    result = calculator->add( i_param1 = p1 i_param2 = p2 ).

    WHEN p_sub.

    result = calculator->subtract( i_param1 = p1 i_param2 = p2 ).

    WHEN p_mult.

    result = calculator->multiply( i_param1 = p1 i_param2 = p2 ).

    WHEN p_divide.

    TRY.

    result = calculator->divide( i_param1 = p1 i_param2 = p2 ).

    CATCH lcx_invalid_parameter.

    MESSAGE 'Invalid input parameter'(dbz) TYPE 'I'.

    RETURN.

    ENDTRY.

    WHEN OTHERS.

    MESSAGE 'Operation is not supported'(msg) TYPE 'I'.

    ENDCASE.

    ...

    ENDMETHOD.

  • 13

    EXERCISE 4 MEASURE CODE COVERAGE WITH ABAP UNIT BROWSER

    Exercise description

    1. Find out procedure and statement coverage your unit tests produce for the report ZCD265_E3_.

    2. Measure code coverage for package TEST_WALLET_TECHED

  • 14

    Exercise solution part 1

    1. Open transaction SE80, select the ABAP Unit Browser (if the tool is not displayed in the SE80, add it via the menu: Utilities Settings Tab Workbench (General) tick the checkbox ABAP Unit Test Browser)

    2. Select Report in the dropdown list and enter ZCD265_E3_ in the input field:

    3. Execute unit tests with coverage:

    4. Open coverage results:

  • 15

    5. Analyze coverage results:

    6. Open Change Layout and select Statement Coverage (%) to display statement

    coverage for the whole program:

  • 16

    Exercise solution part 2

    1. Go to the ABAP Unit Browser 2. Select Favorite in the dropdown list and enter your username in the input field 3. Create a new favorite:

    4. Add the package TEST_WALLET_TECHED to the favorite:

    5. Open Options of your favorite by pressing Show / Edit Options:

  • 17

    6. Select the radio button With Coverage Measurement and save your favorite:

    7. Leave the edit mode:

    8. Press the button Execute to execute unit tests 9. Open coverage results 10. Double click on ZCL_TDD_WALLET_V5 and on the method GET_TOTAL_AMOUNT to

    display coverage highlighting:

  • 18

    EXERCISE 5 DEBUGGER BASICS &LEARN TO WORK WITH BREAK AND

    WATCHPOINTS

    Exercise description

    Part 1: Why are the internal tables itab and itab_copy not equal and which code is changing itab?

    Run report ZTECHED_DEBUG_EX_2 Output: 'ITAB ITAB_COPY'

    Find out why these internal tables contain different content

    In the source code of report ZTSTECHED_DEBUG_EX_2 you find:

    itab_copy = itab.

    if itab_copy = itab.

    else.

    write: / 'ITAB ITAB_COPY'.

  • 19

    Exercise solution

    Display both internal tables in parallel

    Double click on the itab in the variable display to access the table view.

    Create a new table view on this desktop, using the Create Tool icon

    Display table Itab_copy in the second (parallel) table view.

    Expand one of the table view vertically

  • 20

    Diff result:

    Insert itab and itab_copy in the variable view and press the Start comparison button.

    The price differs in line 5 column PRICE:

    Watchpoint on itab:

    Debug report ZTECHED_DEBUG_EX_2 from scratch (se38, Button Debugging)

    Create a watchpoint on itab (no condition) so that you stop whenever itab is changed

    Press Continue (F8)

    You will stop two times:

    1. after select * from sflight into table itab.

    Filling of ITAB

    2. after: skip 2. newline.

    -> switch to the breakpoint desktop and here to the watchpoint tab.

    Use the diff button to compare the old (clone) version of ITAB with the current version.

    Obviously this is the change we are looking for (the price in line 5 was changed!)

  • 21

    Navigate to the source code (ABAP Editor) in order to analyze the code line which changed ITAB[5]-PRICE:

    skip 2. newline

    You may notice that NEWLINE is normally written with a - (NEW-LINE).

    If you double-click on NEWLINE, then you navigate to a well hidden macro definition:

    define newline.

    field-symbols: type sflight.

    read table itab assigning index 5.

    if sy-subrc = 0.

    -price = 1234.

    endif.

    end-of-definition.

    This macro changed ITAB[5]-PRICE.

    Now switch to the table desktop and change in line 5/column price the value back to 1090.00 . (Using the service menu

    of the table view tool:

  • 22

    Now single step to the statement:

    if itab_copy = itab.

    You will reach the correct branch.

    write: / 'ITAB = ITAB_COPY'.

    Press continue to get to the output list:

    Part 2:

    The last task is to find out where this strange text is written to the list:

    First ensure that you run an exclusive debug session (visible in the debugger title: ABAP Debugger Control Session

    1(Exclusive ).

    If not then please wait until an exclusive debug session gets free and then use the menu: Debugger->Exclusive debug modus on in order to switch to an exclusive debugger session.

    Run in the debugger - to the statement:

    if itab_copy = itab.

    Set your cursor on

    write: / 'ITAB = ITAB_COPY'.

    And use menu: Debugger -> Go To Statement in order to reach the correct if branch (or you change the content of itab

    in line 5 again) Set a breakpoint on statement WRITE (use breakpoint button) in order to stop whenever there is a list output.

  • 23

    Now press continue (F8)- after several stops at write statements in the program ZTECHED_DEBUG_EX_2

    You will stop in a OUTPUT conversion exit routine

    You can check the ABAP stack in order to find out where the Conversion exit is called.

    The OUTPUT conversion exit is called during the list-output of field tables_lines (write: / tab_lines.)

    Reason: The Data element zbglines which is used for variable tables_lines refers to a domain ZBGLINES with

    registered conversion exits:

  • 24

    EXERCISE 6 LAYER AWARE DEBUGGING

    Exercise description In the transport system you can register checks like ABAP Test Cockpit, Code Inspector or extended program check which shall run when you check a transport in SE09. Question: If you are in SE09 and check a transport request, which SE09 function module calls these static check tools like the ATC or the Code Inspector? Use the debugger and the Layer aware debugging functionality to find this SE09 function module within seconds. You can use transport request M99K900269 for this exercise: Start Transaction SE09, Display transports of user GEBHARDT, mark M99K900269 and press the check button

    Info: All ATC code resides in SATC* packages

  • 25

    Exercise solution

    Start transaction SE09, display transports of user GEBHARDT. Switch on the debugger (/h) Mark transport request M99K900269 and press the check button. In the debugger switch on layer aware debugging for packages SATC*:

    Press button Configure debugger layer Activate the Layer aware debugging

    Choose Direct definition of visible object set(Layer)

    Insert SATC* in the package selection

    Confirm the pop-up

    Layer aware debugging is now active and you should see in the debugger title Profile active:

    As a result you will now only debug in the SATC* packages. All other packages are invisible during debugging.

  • 26

    Execute one single step (F5) not Continue (F8) ! - and you will reach the first called ATC module (IF_TRANSPORT_CHECK_SERVICE~CHECK (CL_SATC_AC_TRANSPORT_CHECK)) Display the ABAP Stack and switch to the SE09 function module TRINT_INSPECT_OBJECTS which called the ATC module -> Here you go:

  • 2012 by SAP AG. All rights reserved. SAP and the SAP logo are registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo are trademarks or registered trademarks of Business Objects Software Ltd. Business Objects is an SAP company. Sybase and the Sybase logo are registered trademarks of Sybase Inc. Sybase is an SAP company. Crossgate is a registered trademark of Crossgate AG in Germany and other countries. Crossgate is an SAP company.