Skip to Content
Author's profile photo Nabheet Madan

#ABAPUnitTest – TDD implementation

PS:All code can be found in the attachment at end of blog.

Previous Blogs

Finally done with week 2 of ABAP Unit test course, I must confess that I have never-ever written unit test before developing the code. Test Driven Development (TDD) is something which I was not aware of.  When I looked at the first slide I thought this does not make much sense to me. But eventually once I completed week 2, the overall understanding became better and was able to connect the dots.  In order to see how it works with our actual requirement, so thought of implementing it in implicit enhancement. So this blog talks about my experience of implementing a TDD for an enhancement, benefits which I saw, issues which I faced etc. with an intent it will help someone:).

Image from OpenSAP

Before I start, let me also mention one important observation, by sharing my experience here I came to know about so many topics which I was not aware of  for example eCATT for Odata and  ABAP Test double framework.

New Requirement

We have one transaction MB5M which is used to material batches shelf life.  The requirement is to allow only certain types of material to be displayed and rest shall be hidden.

  • To display material types ZPAC, ZEXP and ZFDP
  • To hide material types UNBW and ZAPI.

We are implementing it using an implicit enhancement after data selection which will be used to delete material which are not to be displayed.  Although we also have other enhancement options but I wanted to try out with implicit enhancement

Unit tests to be coded for requirement

We created a basic global class ZCL_SKIP_MATERIAL which will be called in the implicit enhancement.

A custom table with two fields is created to maintain the allowed and not allowed material types. Based on the requirement above, we created one test method MATERIAL_ALLOWED_OR_NOT which will check whether material type is allowed to be displayed or not. Then we created a local unit test class LTC_SKIP_MATERIAL and defined test methods setup and MATERIAL_ALLOWED_OR_NOT.

We got an error for ZCL_SKIP_MATERIAL-> DET_MATTYPE_ALLOWED_OR_NOT called does not exist. So we define it .  Please note that data selection of material types of material and materials is included in the constructor. It can also be separated from this class and later added as injectable, but trying to keep things simple.

So basic set up is done where we have working method which determines whether to display or hide a material based on material type.  So we can see how easy it is to make this productive code from unit testing.

Once this method was up we created one more method PROCESS_DATA which was called from the enhancement. This method iterates through all selected data and call method DET_MATTYPE_ALLOWED_OR_NOT which we have created previously to filter out records.

Benefits which I see

  • One of the most important benefit which I see is I don’t have to run the report each time to see if my code is working for this document type or not, test class does it.
  • Test classes have actually removed the dependency on test data availability in Dev box. Sometimes if test data is not available in Dev box we normally transport to Quality and test it there and fix bugs. But with implementation of local test class basic mistakes and testing can be done at my end without worrying about test data availability
  • Clearly less bugs reported during testing and less time wasted.

Challenges which I faced.

  • Person has to be clear on what will comprise of his/her unit tests and the end result expected.
  • I myself got confused whether i shall fetch custom table data in our method or in constructor, finally chose constructor.
  • Another confusion came whether i shall pass the material data from transaction to class at constructor or via separate process methods. Finally keeping it in process method looks logical, otherwise i will have to first pass during constructor and then update it in process data.

Learning and What is next?

Feel free to provide feedback, open to all ears.

Click here to download code.


Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Michelle Crapo
      Michelle Crapo

      Since I'm not taking the class (Now I will be in the future), these blogs are amazing. Your point of view makes it well worth the read even had I been taking the class.

      Nice one!


      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks Michelle Crapo for the encouraging feedback.

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      Great blog, thanks for sharing! We don't see many blogs on SCN that share the whole story, challenges and all. But this is what I really like and we can learn so much more from this than from an average "step by step with screenshots". And it has references to other blogs. Well done!


      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks for the feedback Jelena Perfiljeva Week 3 is available now lets see what it has in store for us. I also believe that examples related to actual business requirement will help us in understanding the concept in a better way. But yes in this pretty straight forward example it actually worked like a charm. Let us see how complex it can get:)

      Keep sharing your experience and feedback as you always do thanks