#ABAPUnitTest – TDD implementation
PS:All code can be found in the attachment at end of blog.
|1. ABAP Unit Test Week 1|
|2. ABAP Unit Test in Odata|
|3. ABAP Unit Test TDD Implementation|
|4. ABAP Unit Test meets Legacy Code|
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.
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?
- Always Remember The core philosophy Write Test First and then productive code.
- Definitely ABAP Test Double Framework and eCATT for Odata
- Depends on further reading:)
Feel free to provide feedback, open to all ears.
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.
Thanks Michelle Crapo for the encouraging feedback.
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!
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