Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
thomas_jung
Developer Advocate
Developer Advocate
0 Kudos
*Introduction*  A few days ago I wrote a weblog called WebService Navigator Page for ABAP and Java  (WebService Navigator Page for ABAP and Java).  I thought that I had thrown in just about every new technology I could think of.  But as Thomas Ritter pointed out, I could have used the new ABAP Unit Test tool to automate the tests of the Model Class that I performed by hand using the debugger.  I was intrigued by the idea of using ABAP Unit, so I was sure to include it in another recent weblog  (BTF in the real world).  I liked the new ABAP Unit tool so much in fact, that I really felt that I should go back and enhance the original WebService Navigator Model Class by adding ABAP Unit to it.  Not to mention that this gives me the opportunity to try out some of the great online suggestions I have received for enhancing my ABAP Unit Classes.     For those of you who might not be familiar with ABAP Unit, I will give you a quick overview. However I strongly suggest that you search the SDN Weblog area for the term "ABAP Unit". There are several very good overview weblogs on the subject. ABAP Unit is a Unit Testing tool that allows you to build Automated Unit Tests using the ABAP Language. Inside the object you want to test, you declare a special kind of local ABAP Class using the +*for testing*+ addition to the definition. Each method of this class comprises a test case. There are also special methods that you can declare for the setup or teardown of the entire Unit Test or individual test cases. You also have the SAP provided class, CL_AUNIT_ASSERT, that allows you to check your test expectations and throw errors or warnings using its static methods and attributes. Now these Unit Test classes won't even be generated in your production system (controlled by profile parameter  style=" EN-US">abap/test_generation); so you don't have to worry about them endangering your production code.     You can execute these Unit Tests from the Code Inspector (transaction code SCI) or from within SE80 itself.    *Implementation*  This is just my second ABAP Unit class I have written (and both have been written this week). My first one was fairly easy since it was able to test against only public methods and attributes. However the way that I designed my model class in this example, I have tons of processing that takes place in private methods. ABAP Unit Classes weren't going to be much use to me if I couldn't test directly against these private methods. So the first thing I thought of was to just make my local test class a friend of the class I was testing.   Unfortunately that lead to the following syntax error. It appears that only another test class can create a relationship with a test class.   Then it occurred to me. I wanted my test class to inherit from CL_AUNIT_ASSERT so that I could have easy access to all its static methods. Why not then make CL_AUNIT_ASSERT a friend to my Model Class and let the friend relationship inherit down to my test class. Sure enough, that worked like a charm!
5 Comments