0

Unit test which should succeed if exception is raised?

Oct 26, 2016 at 06:13 PM

57

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

10 |10000 characters needed characters left characters exceeded

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").

1

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

0
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Suhas Saha Moderator Oct 26, 2016 at 06:19 PM
2
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
Share
10 |10000 characters needed characters left characters exceeded
Naimesh Patel Nov 01, 2016 at 07:12 PM
0

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.

Share
10 |10000 characters needed characters left characters exceeded

Your answer

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.