Você está na página 1de 5

GettingStarted Newsletters

Hi,Guest

LogOn

JoinUs

Products

Services&Support

AboutSCN

Downloads

Industries

Training&Education

Partnership

DeveloperCenter

Activity

LinesofBusiness

UniversityAlliances

Events&Webinars

Innovation

Browse

ABAPTestingandTroubleshooting

TestDrivingtheABAPTestDoubleFramework
PostedbyWilbertSisoninABAPTestingandTroubleshootingonFeb26,201512:14:25PM
Share

Tweet

Like

SAPhasbeendoingsomereallygoodworkupgradingitstools.Wehaverecentlyupgraded
toSAP_ABAP740.I'manadvocateofABAPUnittestingandthisupgradegavemethe
opportunitytotryanexampleonthenewTestDoubleFramework.PrajulMeyana's
ABAPTestDoubleFrameworkAnIntroductionsaysthatthenewframeworkisavailable
fromSP9.We'reinSP8,butIcan'twaittotesdrivethis.SoIstartedpokingaround.Oneof
mycolleaguespointedoutthatCL_ABAP_TESTDOUBLEisdeliveredwiththerelease.YEY!
Belowisanexampleofbehaviorverificationusingtheframeworkanditappearsto
work.Maybelater,I'llmakeamuchsimplercut.AtthisstageIjustwantedtorunitthrough
areallifeexamplewithinourcodebase.

Belowismyapplicationcode.It'sasimplecustomserviceimplementationtocreateChartof
AuthorityrecordsforOpentextVendorInvoiceManagement.(It'snotrelevantherebutnote
thatweuseFEHtomanageexceptionsforenterpriseserviceerrors.MaybeIcanshowatest
ofthatexceptioninalaterblog.)

Furtherbelowisoneofmytestclasseswithoneofthetestmethodsimplemented.

Thetestdoubleframeworkdoesthreeimportantthingsinthisexample.
Itsetsthebehaviorofget_manager_id()forwhenthetestisexecuted.
Itsetstheexpectedinvocationparametersofset_coa_details()
Theverify_expectation()verifiesthattheset_coa_details()withintheService
interfacehasbeeninvokedasexpected.

Iuseafactoryimplementationtoinjecttestdoubles.Someofyoudon'tlikeit.Iunderstand
that.Hopethatdoesn'tdistractfromtheintent.Havefun.AsImentionedwhenmycolleague
CustodiodeOliveirapointeditoutthatit'savailable,"Let'sbreakit".

AppCode

METHODZIF_FEH~PROCESS.

DATA:
lo_coa_userTYPEREFTOzif_opentext_vim_coa_user,
lo_cx_opentextTYPEREFTOzcx_opentext_service,
lo_cx_coa_userTYPEREFTOzcx_opentext_service,

ls_main_errorTYPEbapiret2,
lt_coa_detailsTYPEzopentext_coa_details_tt,
ls_coa_detailsTYPELINEOFzopentext_coa_details_tt,
lv_manager_idTYPE/ors/umoid,
lv_max_counterTYPE/opt/counter.

FIELDSYMBOLS:
<ls_process_coa_details>TYPELINEOFzopentext_coa_detl_process_tt.

me>_s_process_data=is_process_data.

Store

SearchtheCommunity

TRY.
TRY.
lo_coa_user=zcl_vim_coa_user_factory=>get_instance()>get_coa_user
iv_windows_id=_s_process_datawindows_id

Communications

Actions

iv_active_users_only=abap_false).
lv_manager_id=lo_coa_user>get_manager_id().
CATCHzcx_opentext_serviceINTOlo_cx_coa_user.
CLEARlv_manager_id.
ENDTRY.
LOOPAT_s_process_datacoa_details[]
ASSIGNING<ls_process_coa_details>
WHEREstart_date<=sydatum
ANDend_date>=sydatum."RecordremovedinECCifnotinvalidity
date
ADD1TOlv_max_counter.
ls_coa_detailscounter=lv_max_counter.
ls_coa_detailsexpense_type=<ls_process_coa_details>expense_type.
ls_coa_detailsapproval_limit=<ls_process_coa_details>approval_limit
ls_coa_detailscurrency=<ls_process_coa_details>currency.
ls_coa_detailsbukrs='*'."FunctionalrequirementinECCtoset
CoCodeto*.Assumption:Fromcorp1user=1cocode
ls_coa_detailskostl='*'.
ls_coa_detailsinternal_order='*'.
ls_coa_detailswbs_element='*'.
ls_coa_detailsmanager_id=lv_manager_id."Fornewentries,ManagerId
isthesameasthatonexistingCOAentriesfortheuser.
APPENDls_coa_detailsTOlt_coa_details.
ENDLOOP.

"Ignorethemessage
IF(lo_cx_coa_userISNOTINITIALorlo_coa_user>is_deleted())"The
userisdeletedordoesnotexist
ANDlt_coa_detailsISINITIAL."AND
Alltheinboundrecordsaredeletions
RETURN."Ignoretransactionfinishok.
ENDIF.

"Raisemissinguser
IFlo_cx_coa_userISNOTINITIAL.
RAISEEXCEPTIONlo_cx_coa_user.
ENDIF.

"Updates
IFlo_coa_user>is_deleted().
"User&1isdeleted.COAcannotbeupdated.
""****ZCX_FEHEXCEPTIONRAISEDHERE*****
ENDIF.

lo_coa_user>set_coa_details(lt_coa_details[]).
lo_coa_user>save().

CATCHzcx_opentext_serviceINTOlo_cx_opentext.
"****ZCX_FEHEXCEPTIONRAISEDHERE*****
ENDTRY.

ENDMETHOD.

LocalTestClass

CLASSltc_processDEFINITIONFORTESTING
DURATIONSHORT
RISKLEVELHARMLESS
FINAL.

PRIVATESECTION.
METHODS:setup.
METHODS:test_2authFORTESTING.
*METHODS:test_2auth_1obsoleteFORTESTING.
*METHODS:test_missinguser_coadeletionsFORTESTING.
*METHODS:test_update_on_deleted_userFORTESTING.
*METHODS:test_opentext_errorFORTESTING.

DATA:mo_coa_userTYPEREFTOzif_opentext_vim_coa_user.

CLASSDATA:mo_coa_user_factoryTYPEREFTOzif_vim_coa_user_factory.
DATA:mo_si_opentext_delegauth_bulkTYPEREFTOycl_si_opentext_coa.

ENDCLASS.

CLASSltc_processIMPLEMENTATION.

METHODsetup.
mo_si_opentext_delegauth_bulk?=ycl_si_opentext_coa=>s_create(iv_context
=zcl_feh_framework=>gc_context_external).
ENDMETHOD.

METHODtest_2auth.
**
*Thisteststhescenariowheretheuserhas2authorityrecords*
*andbotharesavedproperly.*
**
DATAls_process_dataTYPEzopentext_deleg_auth_process_s.
DATAls_coa_details_processTYPEzopentext_coa_detl_process_s.

DATAlt_coa_detailsTYPEzopentext_coa_details_tt.
DATAls_coa_detailsTYPELINEOFzopentext_coa_details_tt.

"congthetestdoublecalltomanagerid
mo_coa_user?=cl_abap_testdouble=>create('ZIF_OPENTEXT_VIM_COA_USER'
).
cl_abap_testdouble=>configure_call(mo_coa_user)>returning('WILLIA60').
mo_coa_user>get_manager_id().

"expectedresults
ls_coa_detailscounter=1.
ls_coa_detailscurrency='NZD'.
ls_coa_detailsapproval_limit=200.
ls_coa_detailsexpense_type='CP'.
ls_coa_detailsbukrs='*'.
ls_coa_detailskostl='*'.
ls_coa_detailsinternal_order='*'.
ls_coa_detailswbs_element='*'.
ls_coa_detailsmanager_id='WILLIA60'.
APPENDls_coa_detailsTOlt_coa_details.

ls_coa_detailscounter=2.
ls_coa_detailscurrency='NZD'.
ls_coa_detailsapproval_limit=300.
ls_coa_detailsexpense_type='SR'.
ls_coa_detailsbukrs='*'.
ls_coa_detailskostl='*'.
ls_coa_detailsinternal_order='*'.
ls_coa_detailswbs_element='*'.
ls_coa_detailsmanager_id='WILLIA60'.
APPENDls_coa_detailsTOlt_coa_details.

"configuretheexpectedbehavioroftheset_coa_details()
cl_abap_testdouble=>configure_call(mo_coa_user)>and_expect()
>is_called_times(1).
mo_coa_user>set_coa_details(lt_coa_details).

"Injectthetestdoubleintothefactorywhichwillbeused
insidethemethodundertest.
TRY.
zcl_vim_coa_user_factory=>get_instance()>set_coa_user(mo_coa_user).
CATCHzcx_opentext_service##no_handler.
ENDTRY.

"SETUPINPUTSTotheMethodundertest
ls_process_datawindows_id='COAUSER'.

ls_coa_details_processcurrency='NZD'.
ls_coa_details_processapproval_limit=200.
ls_coa_details_processexpense_type='CP'.
ls_coa_details_processbukrs='1253'.

ls_coa_details_processstart_date='20060328'.
ls_coa_details_processend_date='29990328'.
APPENDls_coa_details_processTOls_process_datacoa_details.

ls_coa_details_processcurrency='NZD'.
ls_coa_details_processapproval_limit=300.
ls_coa_details_processexpense_type='SR'.
ls_coa_details_processbukrs='1253'.
ls_coa_details_processstart_date='20060328'.
ls_coa_details_processend_date='29990328'.
APPENDls_coa_details_processTOls_process_datacoa_details.

"EXECUTEthemethodundertest
TRY.
mo_si_opentext_delegauth_bulk>zif_feh~process(is_process_data=
ls_process_data).
CATCHzcx_feh##no_handler.
ENDTRY.

"Verifyinteractionsontestdouble
cl_abap_testdouble=>verify_expectations(mo_coa_user).

ENDMETHOD.

ENDCLASS.

SomeTestToolsavailableinSAP_ABA740

TestSummary1testmethodsuccessful

TestCoverageonly1test>>soit'sprettypoor

TestCoveragelotsofuntestedcodeinred!


(Sorryfortheeclipsefans.IreflashedmyPCto64bit.Ihaven'thadthechancetoreinstallmyEclipsetools.Those
coveragetoolsaretheretoo!).

2249Views

AverageUserRating
(1rating)

Share

Tweet

Like

0Comments
Therearenocommentsonthispost

SiteIndex
Privacy

ContactUs
TermsofUse

SAPHelpPortal
LegalDisclosure

Copyright

FollowSCN

Você também pode gostar