Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

raising cx_static_check in unit test method

igor_simonovic
Explorer

Hello community,

when I create unit tests and I declare test methods using "FOR TESTING" then I have seen that sometimes is used FOR TESTING with "RAISING cx_static_check" . What is the purpose of this? Every method declared as "FOR TESTING" needs to have also "RAISING cx_static_check"?

Thanks.

1 ACCEPTED SOLUTION

gabmarian
Active Contributor

Hello Igor,

Based on the recommendation if the code under test is capable of raising an exception then the test method itself should not handle it, but declare it in its signature (apart from provoked exceptions), so if it occurs during runtime it will cause the test case to fail.

BR,
Gábor

4 REPLIES 4

p244500
Active Contributor
0 Kudos

HI,

This is we call as Exception Handling in ABAP object

further Exception Categories

Hope this will help to you.

0 Kudos

Hi Nawanandana , in fact I am using class exceptions more then 1 year, ...so you think that the reason for adding "RAISING cx_static_check" is that during design time/coding the programmer just got a syntax warning about not raising a catching the exception? I have noticed in the opensap educ (educ about unit testing and writing testable code) presented by Thomas Hammer and Juergen Heymann that they use this "RAISING cx_static_check" but I don not see they would raise any exception in their code in their test class...so I was wondering about whether SAP unit test framework best practice is to use this raising declaration for each test method declared as "FOR TESTING". I am still not fully sure what was the reason why they have this

"RAISING cx_static_check" in their code. Maybe they explained it in the course and I just missed it.

gabmarian
Active Contributor

Hello Igor,

Based on the recommendation if the code under test is capable of raising an exception then the test method itself should not handle it, but declare it in its signature (apart from provoked exceptions), so if it occurs during runtime it will cause the test case to fail.

BR,
Gábor

igor_simonovic
Explorer
0 Kudos

Thank you Gábor and Nawanandana for your help. I think we can close this topic. I have found also this link :

https://help.sap.com/viewer/ba879a6e2ea04d9bb94c7ccd7cdac446/7.40.16/en-US/dd587324e2424b14ab5afb323... which tries to explain the use of

Handling Exceptions in ABAP Unit Tests. I have created also a method raising (code under test) where I raise exception and I have created test method for this cut. In the test method , in declaration, I had to add "RAISING zcx_…." otherwise I have got a warning in Eclipse that I need to either add "Raising..." or catch the exception in the test method. Same warning I get for another non-test method. So ABAP here behaves in same way for test methods and non-test methods. In my test method I declare "RAISING zcx_…." and not "RAISING cx_static..." since I think that raising the concrete exception is better. Maybe I would use "RAISING cx_static..." if I would have several exceptions in cut, to avoid raising several concrete exceptions.

In the link above I see that they catch their zerodivide exception before using

cl_abap_unit_assert=>fail... In my test method I catch my zcx exception after

cl_abap_unit_assert=>fail... because I found out that this works then correctly. For me, it looks now like a mistake in the link above if they catch exception before cl_abap_unit_assert=>fail...