2023 Jul 19 10:26 AM
Hello,
I am trying to write a unit test to cover all the executable statements for the below method; METHOD create_file_id.
TRY.
rv_file_id = cl_system_uuid=>create_uuid_c32_static( ).
CATCH cx_uuid_error INTO DATA(lx_uuid_error).
RAISE EXCEPTION TYPE cx_csc_runtime
EXPORTING
textid = cx_csc_runtime=>fileid_creation_error
previous = lx_uuid_error.
ENDTRY.
ENDMETHOD.
I am unaware on how to handle the executable statements under the catch block.
Thank you in Advance,
Rakesh
2023 Jul 19 1:47 PM
Hello rakesh_aithal ,
to be able to do that, you need to replace your static method call:
rv_file_id = cl_system_uuid=>create_uuid_c32_static( ).
Because it is static, you can't mock it. So you want to create an new class/interface "zcl_system_uuid", inside this class you can use the static method call. And now, with the known dependancy injection methods, you can control the code under test and raise the exception for your unit test.
Cheers,
Chris
2023 Jul 19 12:40 PM
2023 Jul 19 1:47 PM
Hello rakesh_aithal ,
to be able to do that, you need to replace your static method call:
rv_file_id = cl_system_uuid=>create_uuid_c32_static( ).
Because it is static, you can't mock it. So you want to create an new class/interface "zcl_system_uuid", inside this class you can use the static method call. And now, with the known dependancy injection methods, you can control the code under test and raise the exception for your unit test.
Cheers,
Chris
2023 Jul 20 7:37 AM
OK, you mean how to mock UUID creation...