2022 May 24 9:41 PM
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!
2022 May 25 7:19 AM
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.
2022 May 25 12:13 PM
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.
2022 May 25 12:27 PM
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' ).
2022 May 25 12:30 PM
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.
2022 May 25 12:42 PM
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.
2022 May 25 8:33 PM
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
2022 May 25 9:16 PM
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
2022 May 25 9:28 PM
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
2022 May 30 6:36 AM
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
2022 May 30 9:18 PM
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