Skip to Content
Author's profile photo Thomas Jung

WebService Navigator Page for ABAP and Java: Part 2 – ABAP Unit

*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. image   *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. image  Unfortunately that lead to the following syntax error. It appears that only another test class can create a relationship with a test class. image  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! image

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Peter Inotai
      Peter Inotai
      Hi Thomas,

      Thanks for the weblog, it gives some idea about ABAP Unit...unfortunately I'm still working with 46C, but ABAP Unit seems a quite useful and powerful tool.

      BTW: Did you use the integrated ITS for the screen shots?

      Cheers,
      Peter

      Author's profile photo Thomas Jung
      Thomas Jung
      No I didn't take my screen shots with the Integrated ITS.  They were taken with my Windows SAPGui.  Why do you ask?
      Author's profile photo Peter Inotai
      Peter Inotai
      I never seen this kind of style before, that's why I was wondering.
      Author's profile photo Thomas Jung
      Thomas Jung
      Actually I get that question a lot.  I might have to share my information on the tool that I use for WinXP themes and how to setup the SAPGui to accept them in a weblog.  Not exactly hard hitting information, but fun none the less.
      Author's profile photo Christian Lahmer
      Christian Lahmer
      Hi,
      if i understood it right, you haven't found a solution to make a lokal class a friend of a global class! Didn't you?
      But it is not that difficult. Ok it is not well documentet but it works. You have just to add the following statement into the local area of your class:
      * announce global class
      class lcl_my_unit_test definition deferred.
      * define friendship
      class CL_ICL_GRID_INVALIDITY definition local friends lcl_my_unit_test.
      Regards Christian