Application Development and Automation 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: 

Succes message in Unit Tests

1,509

Hello 🙂

I have a problem regarding success messages in my unit test. How is it possible to mock them that in the unit test the message Statement isnt excecuted?
For errors and warnings i just raise an exception catch it outside the cut and display the message with if_dyn_message (like in the answer of https://answers.sap.com/questions/13031300/-a-doubt-on-message-handling.html).


Does anybody know how to deal with sucess messages? I mean i could raise an exception too as for errors and warnings but i think its like an antipattern to raise an exception for a sucess message haha.

One approach could be to create an Interface and class for the display of sucess messages and mock this in case of unit Test, so the message is not sent, but its not the best...

Thanks in advance!

10 REPLIES 10

FredericGirod
Active Contributor
1,138

Could you provide a simple example ? For me, in the Oo programing, there is no success message. There could be a success return, but it is easy to catch.

METHODS do_my_action
IMPORTING
my_input_parameter_1 type something
my_input_parameter_2 type something
RETURNING
VALUE(success) TYPE abap_bool.

0 Kudos
1,138

Hi Frederic,

my structure looks like this:

Try.
object->excecute( ).
Catch my_exc_class into data(gx_obj) Message gx_obj type gx_obj->if_dyn_message~msgty.

The Method excecute looks like this.

Method excecute.

me->do_some_checks( ) "with raises exceptions sometimes

if me->create_both( ) eq abap_true.
message s001.
elseif me->create_one( ) eq abap_true.
message s002.
endif.

Endmethod.

Sandra_Rossi
Active Contributor
1,138

Why don't you mock it as method? It's not specific to message, you do a test double of anything you want.

message( msgty = 'S' msgno = '001' ).

FredericGirod
Active Contributor
1,138

I don't answer your question, but for me you are bypassing the SOLID principle.

Your method should do only one thing at a time. And you are doing two things: The action, manage the output.

Method excecute.
me->do_some_checks( ) "with raises exceptions sometimes
result = COND #( when me->create_both( ) eq abap_true
then zcl_my_subject_constants=>thing_execution_both_created
when me->create_one( ) eq abap_true
then zcl_my_subject_constants=>thing_execution_single_created ).
Endmethod.

FredericGirod
Active Contributor
1,138

And for your question (not nice solution, but it works):

CLASS to_be_tested DEFINITION.
PUBLIC SECTION.
METHODS do_something
IMPORTING
my_input TYPE abap_bool
RETURNING
VALUE(success) TYPE abap_bool.
ENDCLASS.

CLASS to_be_tested IMPLEMENTATION.
METHOD do_something.
IF my_input EQ abap_true.
MESSAGE ID '00' TYPE 'I' NUMBER '358'.
ELSE.
MESSAGE ID '00' TYPE 'E' NUMBER '358'.
ENDIF.
ENDMETHOD.
ENDCLASS.


CLASS to_check DEFINITION FOR TESTING RISK LEVEL HARMLESS.
PUBLIC SECTION.
METHODS check_abap_true FOR TESTING.
ENDCLASS.

CLASS to_check IMPLEMENTATION.
METHOD check_abap_true.
DATA(utc_result) = NEW to_be_tested( )->do_something( abap_true ).
DATA(msg_result) = CORRESPONDING syst( syst MAPPING msgid = msgid msgno = msgno msgty = msgty except * ).
cl_abap_unit_assert=>assert_equals(
act = msg_result
exp = VALUE syst( msgid = '00' msgno = '358' msgty = 'I' ) ).
ENDMETHOD.
ENDCLASS.

0 Kudos
1,138

Hi Sandra,

yes i could do this but i will need a new Interface and class for that, which i can replace with a testdouble. Thats what i described in my approach at the end. I thought there could be an alternative 🙂

Thank u

0 Kudos
1,138

Hi Frederic,

yes youre right my coding is a little bit more complex, i just wanted to show my problem 🙂

Its more like this:

Method excecute.

do_some_checks( ). "Maybe exception

if should_both_be_created() eq abap_true.
create_both() "with exception or sucess message inside
elseif should_one_be_created() eq abap_true.
create_one() "exception or sucess message
endif.

Endmethod.
You mean i should split the displaying of the message an the processing of the creation? Like:
If create_both() eq abap_true.
message i000.
endif.
Thanks

0 Kudos
1,138

Hi frdric.girod

thanks for your Code! Isnt it a problem that in the unit test the message statement with type error is excecuted?

The assert equals sounds understandable 😉

Best regards

FredericGirod
Active Contributor
1,138

It is a big problem 🙂

But, for me it is forbidden to manage MESSAGE statement in a class. You should have an Exception to manage error.

And exception is easy to manage with ABAP Unit.

try.
cl_abap_unit_assert=>assert_equals(
act = ctu->my_beautiful_method( my_parameter )
exp = value what_ever( nothing ) ).
catch my_exception_class into data(o_exception).
endtry.
cl_abap_unit_assert=>assert_bound( o_exception ). " <-- I am waiting an exception

0 Kudos
1,138

Hi Frederic,

for errors i use exceptions and this works fine. I tested messages Type I and S with the message statement not mocked and funnily enough my unit test is green. But to be safe i will mock is as a method like sandra.rossi mentioned.

Thank u both!

Best regards

Julian