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: 

Unit test which should succeed if exception is raised?

nuno_godinho
Explorer
0 Kudos

Hello,

I am trying to write a unit test which should be successful if the method being tested raises an exception.

The constructor MUST raise an exception if the provided structure doesn't exist in the DDIC.

This was my attempt:

TRY.
    CREATE OBJECT o_field
      EXPORTING
        i_structure_name = 'I_DONT_EXIST'
        i_field          = 'BUKRS'.
    
    cl_abap_unit_assert=>fail( msg = 'Invalid structure not detected' ).

  CATCH zcx_field.
ENDTRY.

Test should be successful if the exception is raised.

But unfortunately I can't seem to make this work since the unit test will automatically fail as soon as an exception is raised. SAP says it quite clearly here:

http://help.sap.com/saphelp_nw74/helpdata/en/dd/587324e2424b14ab5afb3239a77a8d/content.htm

"First of all, ABAP Unit captures unexpected exceptions, even non-catchable exceptions. If an exception occurs, the ABAP unit test fails and the exception is reported as the cause of the failure."

Isn't there a way around it?

How can I write a test which should succeed if an exception is raised?

Thanks in advance,

Nuno

1 ACCEPTED SOLUTION

SuhaSaha
Advisor
Advisor
But unfortunately I can't seem to make this work since the unit test will automatically fail as soon as an exception is raised.

Is the exception defined in the signature of the CONSTRUCTOR?

  • If NOT, then the unit-test will fail
  • If YES, then handle the exception in the CATCH and then check the exception object with the IS_BOUND( ) method
4 REPLIES 4

SuhaSaha
Advisor
Advisor
But unfortunately I can't seem to make this work since the unit test will automatically fail as soon as an exception is raised.

Is the exception defined in the signature of the CONSTRUCTOR?

  • If NOT, then the unit-test will fail
  • If YES, then handle the exception in the CATCH and then check the exception object with the IS_BOUND( ) method

Sandra_Rossi
Active Contributor

No, impossible. Probably you didn't do it correctly (c.f. Suhas' answer).

"First of all, ABAP Unit captures unexpected exceptions, 
even non-catchable exceptions. If an exception occurs,
the ABAP unit test fails and the exception is reported as
the cause of the failure."

"ABAP Unit captures unexpected exception": it means probably that the AUnit framework catches exceptions using CATCH cx_root.

"even non-catchable exception": classicaly, it always leads to a short dump, so it's achieved using a Kernel/non-ABAP feature (the only way to mimic it is to execute code via RFC, there will be a short dump but the control goes back right after the RFC call, with classi exception "system_failure").

nuno_godinho
Explorer
0 Kudos

Hello,

Thank you for your answers. Indeed I must have done something wrong. It happened twice today and after reading that sentence I assumed it was impossible by design (which did seem odd). But, in fact, the sentence doesn't say that it catches all exceptions. It says it catches the unexpected ones. I misinterpreted it.

After your replies I confirmed that the exception was properly declared in the method and added a break-point to the CATCH statement. It did stop there after all. Somehow... I removed the break-point again and it was working fine.

And now it works.

So... for sure I was doing something wrong... but now I will never know what!

Anyway, thank you for your answers because due to them I understood that this should work. And now it does work 🙂

Regards,
Nuno

naimesh_patel
Active Contributor

Its a good practice to keep both logic separate in your unit test method - The one where you call the method of CUT (code under testing) method, and the Unit Test Assert.

So for your testing something like this should give you correct unit test result.

* CUT 
TRY.
    CREATE OBJECT o_field
      EXPORTING
        i_structure_name = 'I_DONT_EXIST'
        i_field          = 'BUKRS'.
  CATCH zcx_field into lo_exc.
ENDTRY.


* Assert
cl_abap_unit_assert=>ASSERT_NOT_BOUND( act = lo_exc msg = 'Invalid structure not detected' ).

Thanks.