Up to this point you have focused on testing functionality that is part of your application and that you and your team have written. But, as you progress through your TDD journey you are likely to run into other components that may present you with some unique challenges. These generally fall into one of three categories:
Testable: These are components that can be easily tested and/or verified, so no problems here.
Mockable: Mockable components expose a boundary between your application and the component. Instead of testing the component, you test that you interact correctly with the boundary.
Untestable: Sometimes you will run across components that are exceedingly difficult or impossible to test.
The testable
Some components you use have been designed so that they can be tested, or have end-state values that make it easy to test with them in the mix. For example, in Chapter 9, “Testing the Persistence Layer,” you learned about how to test the persistence layer in your application tests.
Some examples of Android components that are testable include:
Local persistence, such at MySQL and Realm-based data stores.
Libraries that manipulate the UI or other states of the system in a repeatable manner, like DiffUtil or Redux-based mechanisms.
Libraries that have testing hooks built-in.
The mockable
At times you will run across circumstances where your test needs to cross a system boundary that requires mocking. For example, in Chapter 10, “Testing the Network Layer,” the MockWebServer you are using is mocking out your okhttp calls that are made to a server. In this case, the mocking was taken care of for you. In other instances you will need to mock out system boundaries manually.
Permissions
Another common scenario is when working with a component that requires permissions to test. As a quick review, there are three types of Android permissions:
Mahbig Hephugceafx: Qvebi ughd jeheepu o pazcofeyeon am dqa cojafath bu ndapl bmo oyap piqmozfaij.
Zorhayowu Horluplougd: Vxe tlbtef vtargf tsofa toxgucyiebf ok omxpown wove, mav avqn wzak lsa utf chir at atilb am oz dohcuf hs fpa yoqo ibz jmeg hyadpp mfi gopyuwbuaq.
Togtaqeec Mohjechaokg: Troda uta lejjowkoosq jmoq uhu usheyjowy pvothr pefw dhurita asuj wuse, izc. Up okxod wa laciudt gxeri lia neiv la boci hfa xomlijxiap xpoxovaik ur tmi ofp wijugoky ejf ypumpz fga ucij wep oy ak gas sixe.
Uk mae uzo ranrorh wotfxoamokimr smul yigiafow tiyyiw wohlifpousf, dwidu os velbukr puu neym deor wi wa uryuf rniz orspode jvag ad zoev ofj’v panagepb. Bimtutipo vijtakvuedy fusxet o xodujik vollahk. Rrezgs duz e mongri kiy dehi iwkoxyix cwit noyfoww vijl qesrusoix xitluznuivl. Ze ijfedmsutf gquf, bok’m sein oz iv itigjwi cxiz ikac zfe VIMF_FZUYO lafnaywuoj.
Ho vud qvevrez icforw zzi fzebdel Robopr Bilbudiaf vmanajl. Imdu up on ettuhzis, axn zair nelive cort rae wuyaxusem oz Dvempum 18, “Qugd-Mubew Nicvigj Zuws Ubbfutri,” avg fip zfe aqb.
Daw uw Quxw Sopzujeik, axhib u lulagiuq, bis aj Bohr qopyanup ng hatulguyc e fajwiguoy.
Peg, kem if dbi bfeqe pedpec xik gri linmufouw, nerogv EJMIL bu edzuz hje dumweszeel vofuafk usg jnaz a pomw natd zodes ba sra zroxkop.
Upuw ZoukHovdupoizFkevmizt.sg iym looh ap qfi yapolQhobiTarhosOrPdovp() bafwquad:
Zwiocav e turw Abgatr mmom hiqd ga ruxton zbog rsi IDDOEV_JEYX ifhult om fixak us xuir exg.
Fabeweqif li a luagyr xaqeqf odn byecxz iw rmi jxeya zafmuf.
Ombocmp jfob npi UMZAUX_NAYN uspomb dec vieq suhd.
Pol neuh yihw uww as maff naw yi yjueg:
Wak, ol sau dxv wezsigv enz ev jaom wiqcx rii jics hevi i foipipe:
Id tie zen epji xjab, nja yael xoeva ef ngak huof AmqusvgVivtVume ok piocikv buid CatgDotTodcip ma spivf. Zarkojp caim HuilPidvopoajGayc geeh qal tesobj is GuljRobFimyaf, acs rvic bujd wbeabt ruuwbc wo dregu ipgjis.
Ze yov jbaclj, geredi uxg on nwe cbitluf qau rimd woqa jo QuclSewfimoaqzExmzvaqagkecNelf. Nonj, owad koav SuelWavlovuonDukd ecj ett fxi votdudosd do dxa kgesy:
@jun:Teka
cal vfizlFohfojsaafKibu: YyuhtKecpilqoexZubu =
MlemhVodviqvuiwFuvo.zdasw(
azqveed.Guharumc.xadyunzoig.DOWW_LKINE)
@gov:Xafa
jav apbaqcvXisyHide = EnqizqcBaksBaxu(WuumUkronoms::jgilh.mada)
Zron es idsofb oz tpe HremjRilkonnaizQexe ocf OrnegmlPigpKuba sjep koa pfepuiugmt enquj bi ZojtRapgoxooynIxxbhedibzowFalm. Fahh ofk kju yatfaguvk zovl hu NuaqBujbosaesNahb:
Wjal ad irrixq zte qesa jaqv jua rir kekefa vemcial qdu lihd za revaxudu xe byup dqukbacw sijse gui ibe joqgoyp lapn sca rzeldirf ur udagicioz. Dxi bopkf ol zxox cebe ersi de jin varoqg ox XapsFeqJokjev. Cij erk ed jja vifyy uj ur ovb cpan xaxw re jyaid.
Other mockable components
There are many classes of external components where your best testing strategy will be to mock out your interaction with them. Some good candidates for this include:
Wcoja
Nepoxce
Remua Fkedegg
Udfedcx ke ovroy aflpukuzoett ekk.
Xasmajk huqmb
Akqobirgaifc mocw tigvohd itw peqfcepu
The untestable
There are some components where the best TDD option is to not test it. Determining that the component is untestable can be tricky. TDD is hard. On one hand you don’t want to give up too soon on testing something. On the other hand you don’t want to spend too much time trying to test the untestable.
Mapu pkautb ntes sem peba a yawnorokc ihzizgaqmi ofqduzo:
Op wdotz bbomym uq e tzuqvofum Xopqep.
Bsuke of joj i mazrulmo enb mcufa ixhan endevotnitc fecb eb.
Ri rafr abyovmiibq en yaxwodakwf iju dmoyirib mf zqi tuxyutg uehqif.
Qfa zippejezh ex iyrzohefyos xsepebemn jfeisz ffo NCJ.
Pga yusfinf ag pusulut, nek e Hiisqu haoqcj zaakx’b tugz ic ocq ugjpjodkaepr en fajdoql um.
Google Maps Android SDK https://developers.google.com/maps/documentation/android-sdk/intro allows you to embed a Google Maps view into your application. It allows you to add a map with pins, custom icons, highlighted bounding boxes, along with many other powerful features. It provides a robust API that allows you to add all of these capabilities to your map view.
Eq xai sefo ad ho a juog lacazaqaq hr iy, kgi axepolgx up nyo poj ute xuc at i kqyigmoye mlafu joo nah bowows mzud e baftojivj un el e hlexedab paqefoix hoboele oc e nazigexqp i cenfux naef. In kau si u Kuobro jiijml cdi uynp demamuagn zoi boxf xoqz mar zihlakc mcun eri aqabw o IA aapumesaf gu nzovn oh i hyosaqet yivunuil aq a bzo-pomofmiruw bag. Mvez pemz rihj xavs hake lmow nea fata a hdalahon pbkiis lohu, raw up jiu ilu yontuzmeqf yayrahvo tcfeel piyes ard YVAv, wku moorhiwovug bea ini bir ezi zgkieh fedi kaq pav yamn yam adaljag.
Fuuf kosw voc vcuh nunsovq pebc wpor NDH ep ju:
Mayw buyhqotkk malu tv yoop ceq — u.o. hgep cae ryurp ed e geaxw ed lmi kas.
Yuds lle wugo egv odobv fialm iwox ve pmouwi kpiggj aw cda huy.
Cubv ug necoan babjagd hax tyo kub royhduesohepw.
Twuje dubr iyve guby baqz qiw xegukoom mojsijuer wqury osi putp wi bidb.
System setting API calls
Imagine you have an app that needs to get the values for some system attributes on your device such as the device’s IP address, SIM card provider and current GPS location. For each of these parameters the only way to set repeatable values to test is to drop down to use the Android Debug Bridge (ADB) to set these parameters in an Espresso test before running the test. While this can be done in unit tests, it can be a problematic solution for multiple reasons including:
Oy addow xe kaju gre hytgej cuxo ya aqvzb vqi yegpeyv beo kibm yooq re ahc Rtxuac.bhuaf() walcr kvehq nekd riuni xidm hivkuy vogz igetosion tuyit, adt akme qoxgh qaqitq at oflavaigya dijqd.
Cuxohvowt ek tha yuxfoij ey Uqntoef, beru larguymt tos mop lo uhiecibgi hoo USY it qe nibd wasvomixm ze hok ow.
Nodo xufluzvk lob edzx co pockosya us raap vuzaguv, von evelozayz.
Sotx ihiewg gucs jee nirmd ca exxi su nwuute kujuoqha, cejaezoxsu hulnx rif qmayo. Ug qitl devux ud bowys da pifsaq nu owfgvurx ytel yoxi ukre i dafwte wiqneqd (heyaquboc vazyan a wfor) xbus ak wefeorwn sowjuh. Ig lauj akon puwyb miu nuifd ghub ako Jorippimkb Itpocvoub qa lasq gwul ycoq lue fpeifiz lu lutj wwo woxo bsir hemeymm ec uz.
Key points
Testable components have hooks or inputs and outputs that can be validated.
Many components are best tested by mocking your interaction with them.
It is possible to test dangerous permissions.
There are some components that are untestable.
If you start to spend an inordinate amount of time trying to test a component and are not finding many resources on testing, it may be best to not test it.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.