Requirements Documentation and ABAP Exercise Programs
Total Page:16
File Type:pdf, Size:1020Kb
APPENDIX A Requirements Documentation and ABAP Exercise Programs The source code for this book is available on GitHub via the book’s product page, located at www.apress.com/9781484269503. It contains the following: • The file containing the requirements document describing the associated ABAP exercise programs (.pdf). • The file containing the supplemental source code described by the requirements document to be included in each new exercise program (.txt). Use this as the source for the code to copy and paste into each new exercise program because, unlike .pdf documents, it retains consecutive spaces used for maintaining proper formatting and alignment of the ABAP code. • The files containing the source code for each of the associated ABAP exercise programs (.txt). • The file containing the ABAP program for uploading all the exercise programs into your training system (.txt). 373 © James E. McDonough 2021 J. E. McDonough, Automated Unit Testing with ABAP, https://doi.org/10.1007/978-1-4842-6951-0 APPENDIX B Answers to Chapter Quizzes Answers to Quiz #1 Presented at the end of Chapter 4 Multiple Choice: Select the Best Answer The correct answers to the multiple choice selections of Quiz #1 are the entries highlighted in bold. 1. xUnit describes A. Manual code–driven testing frameworks B. Automated code–driven testing frameworks C. Consolidated code–driven testing frameworks 2. xUnit enables testing at the A. Internet level B. System level C. Module level 3. xUnit facilitates A. Test-Driven Development B. Extreme programming C. Seat-of-the-pants development 375 © James E. McDonough 2021 J. E. McDonough, Automated Unit Testing with ABAP, https://doi.org/10.1007/978-1-4842-6951-0 APPenDIX B AnSWerS to ChaPter QUIZZeS 4. xUnit tests are implemented as A. Breakpoints B. Conditions C. Assertions 5. xUnit facilitates preparing a test through A. Dynamic definition B. Fixture C. Collection 6. The order in which xUnit tests are executed A. Is the order in which they appear B. Is dependent on test attributes C. Should not matter True or False The answers to the True or False statements of Quiz #1 are shown to the right of the statements. Advantages of using xUnit testing include 1. No need to remember what the test result should be True 2. Elimination of user testing False 3. Tests are automated True 4. Reduction in requests for changes False 5. No need to think about how to implement logic False 6. No need to write the same test more than once True 7. Can substitute for design discussions False 8. Enables testing of peripheral systems False 376 APPenDIX B AnSWerS to ChaPter QUIZZeS The phases of xUnit can be described using the word sequence 9. Ready, Set, Go False 10. Arrange, Act, Assert True (Osherove) 11. Setup, Exercise, Verify, Teardown True (Meszaros) 12. Open, Test, Close False Answers to Quiz #2 Presented at the end of Chapter 5 Multiple Choice: Select the Best Answer The correct answers to the multiple choice selections of Quiz #2 are the entries highlighted in bold. 1. ABAP Unit tests are written in A. SAPScript B. Java C. ABAP 2. ABAP Unit tests A. Must be implemented as local classes B. Must be implemented as global classes C. May be implemented as either local or global classes 3. ABAP Unit tests can be executed A. Only from the editor B. Only from the Code Inspector C. From either the editor or the Code Inspector 377 APPenDIX B AnSWerS to ChaPter QUIZZeS 4. An ABAP Unit test class definition requires the class to be A. Marked as “for testing” B. Inherited from a globally defined static class provided by SAP C. Defined in a separate module 5. An ABAP Unit test validity is asserted by A. Using an ASSERT statement B. Invoking static methods of the class cl_abap_unit_assert C. Calling function module ASSERT_THIS 6. An ABAP Unit test may test A. Only code written using classes and methods B. Only classic procedural ABAP code C. Classic procedural ABAP code and code written using classes and methods True or False The answers to the True or False statements of Quiz #2 are shown to the right of the statements. An ABAP Unit test may be defined for 1. Executable programs True 2. Class pools True 3. Interface pools False 4. Module pools True 5. Function groups True 6. Configuration False 7. Subroutine pools True 8. Type groups False 378 APPenDIX B AnSWerS to ChaPter QUIZZeS An ABAP Unit test 9. By default is compiled into all environments False 10. Accommodates using a fixture True 11. Is embedded with the object to be tested True 12. Can generate the source code to comply with the test False 13. May accept parameters False 14. Is transported along with its tested object True 379 APPENDIX C Concepts Associated with Defining Local Test Classes Although the ABAP Unit Testing Framework requires that a unit test be written as a local class, such a class requires the use of only a few of the many object-oriented ABAP statements. This section covers those concepts of object-oriented programming necessary to be known in order to define and use local test classes effectively. If you are reading this section, the assumption is that you know nothing about either object-oriented programming or the ABAP statements used to write object-oriented programs. We will concentrate only on what is required to be understood in order to use the object-oriented extensions to the ABAP language to define and execute local test classes.1 With object-oriented programming, the fundamental unit of design is called a class. A class is a complex data object consisting of attributes and behaviors. Collectively, attributes and behaviors are known as members of the class. An attribute of a class is simply a data item. Each attribute is defined as a data field or as a constant, using the ABAP statement DATA or CONSTANTS, respectively. The TYPES statement also may be defined within the class to assist in defining the data fields and constants. A behavior, also known as a method, is an action defined for a class that can be used to read or change the values of its attributes or to produce some other type of processing result. A method must be invoked to perform the action it provides, similar to using the PERFORM statement to call a subroutine that performs some action. 1For a comprehensive explanation of using object-oriented design in ABAP programs, refer to Object-Oriented Design with ABAP: A Practical Approach (McDonough, James E., Apress, 2017) 381 © James E. McDonough 2021 J. E. McDonough, Automated Unit Testing with ABAP, https://doi.org/10.1007/978-1-4842-6951-0 APPENDIX C CONcEPTS ASSOcIaTED WITH DEFINING LOcal TEST ClaSSES The following example, from the SAP online help,2 shows a simple production class named myclass and a test class named mytest3: * Production classes CLASS myclass DEFINITION. PUBLIC SECTION. CLASS-DATA text TYPE string READ-ONLY. CLASS-METHODS set_text_to_x. ENDCLASS. CLASS myclass IMPLEMENTATION. METHOD set_text_to_x. text = 'U'. ENDMETHOD. ENDCLASS. * Test classes CLASS mytest DEFINITION FOR TESTING. PRIVATE SECTION. METHODS this_test FOR TESTING. ENDCLASS. CLASS mytest IMPLEMENTATION. METHOD this_test. myclass=>set_text_to_x( ). cl_abap_unit_assert=>assert_equals( act = myclass=>text exp = 'X' ). ENDMETHOD. ENDCLASS. As with all classes defined using the ABAP language, each class has both a definition component and an implementation component. The definition component of a class specifies its attributes as well as the names and signatures of its methods; it begins 2https://help.sap.com/saphelp_snc700_ehp01/helpdata/en/49/180619005338a1e100000 00a421937/content.htm?no_cache=true 3With this example, I have taken the liberty to change the name of the method defined in class mytest from mytest to this_test simply to avoid confusion with a class and one of its methods having the same name 382 APPENDIX C CONcEPTS ASSOcIaTED WITH DEFINING LOcal TEST ClaSSES with the CLASS … DEFINITION statement and ends with the ENDCLASS scope terminator statement. The implementation component contains an implementation for each of the methods specified in the definition component; it begins with the CLASS … IMPLEMENTATION statement and ends with the ENDCLASS scope terminator statement. The statements METHOD and ENDMETHOD are functionally equivalent to the statements FORM and ENDFORM found with classic ABAP. Both the definition and implementation components together represent a complete class. In the preceding example, class myclass has been defined to contain a static attribute (CLASS-DATA) called text and a static method (CLASS-METHODS) called set_text_to_x. These are both defined in the PUBLIC SECTION, which assigns public visibility to any attributes and methods following this section header. Public visibility means that any external entity has access to these members – can read or change the attribute and can call the method. Effectively, external entities can see these class members. However, in this case the attribute text also carries the READ-ONLY qualifier, which means external entities can read its value but cannot change it. The implementation for its method set_ text_to_x uses a simple ABAP assignment statement to set its attribute text to the value ‘U’. This is about the simplest example of how a class could be defined which has both an attribute and a method. By comparison, class mytest has been defined to contain no attributes but with only a method called this_test. The method is defined on a METHODS statement in the PRIVATE SECTION, which assigns private visibility to any attributes and methods following this section header. Private visibility means that only the class itself has access to these members – can read or change the attribute and can call the method. Effectively, external entities cannot see these class members and so have no access to them.