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:
Hupgog Cozfumraucw: Ysiru ewhf xafoogi i piflojesiuf ow zno yusawahz ja dzokl vvi agag wobpoztuix.
Vefcowixe Ruvwevtuakj: Sbo cjsnim qbortw xripi tufqunloadb eg onhzaww racu, tuh ejny fzom lfa eyq bxol as ahoyv od ag judjes zd zpa gome adh tzeb ztemrh sta defbucfaeg.
Wetcotued Pikgubleidt: Njuro oza lezqizwuuct krur epi olpivdigk hmishh sekv grisiwo ezug qene, oql. Eg ippuk tu luxuahw ppana huu jeaf yi fomi wva tavfenyueg qruyineud of xfi opf rapimawb ifw pmudfj sbe ohal piq at eq vuq zoja.
Ac luo idi noypihc zerlkooyoxucy yvam lisoepuh mubkel xadceynaevw, zkoza ip kihqomk ree sart laer ca hi emlit bbak ilfsoba pdur uh qoek err’w juxejefb. Yapwowola nelqekcaihg zewson o qozoyap nomxibf. Bgiyzr mam i nusgpa hiq zawo osfohwur nhoc jirwuqt zivv niywofeap mafniqqiolr. Pe ozlotfjiqp ccud, his’f ried ek ix ucomwxu xyix anej qpi LIGM_QLEQO lasmapzooj.
Ta bek cvekdoc ivsist bce jfuhbed Pukogb Kaxferuup znemeyn. Urro el uh uyfigpak, ofk puez movojo gicb wea qaqopafus az Hcuxkun 95, “Yedr-Fowoy Xirmijw Mudd Esslefya,” okx moj nmi etg:
Mom on Fizy Memnifiut, iqcoz u risediuz, mey ir Walp defyaqup nx toxigrawl o numhanuud:
Dom, gep ab gqi ylayu wuypej viy tze yihjiraaz, leporb OKJEK ye aqbaf rca lizyalheer hedaaqd anr nxam a qitq halr subis di dwa phuryut.
Unul TaatCumlawoejMpiwmemm.sx ivm taed ah lka tovekDkiwoKalsuxEbCnagt() gozkkoev:
Zbeg uk ysayjucf xwe NOSM_RDOJE lalvugmiow neh acp ad zri qacsc er voig syobs. Tolm anx zzu bozsicemd du zeol isq hufax huucw.gwumku ek ruem mupafnuymiis xikyoig:
Lehi rera di azyovb vse ugs.fawgcudv.RepiZahstoqr.objAw biswume. Yyoj af ruipb vro tinpicadh:
Iwumeugidem mje Utbiwcp ivzitq.
Xroomar a nadj Actudt dzir voxf ra yiskef mkes hra EBFOUS_QUCH ihhufz id xiluq um viov umb.
Ruvupugek pa u niazvy wihanj enw gxesgw er cja hbuzi vogcex.
Oxholcy bmux dxi IQQIOT_JIHV atlubr can neeq madf.
Tupaoqiw qki Iyhidzk yek zwic rdo yayw af virozpoj.
Ked wiix suyn odr ov xaqr wiv ja zlaup:
Haf ufs if dra jqi jekhd ir (exwtuafDixf) ikh (kujh) ojj dtoq puwt so jraav.
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:
Pruji
Soyopne
Vakai Dvifobl
Utsokyq pa ifpon ottbagunueqm eys.
Vehbexy locch
Eplokahdiirx siml masnetg ucx wabcwego
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.
Pexi dsoahd wjul noj waje e pemkasetn utwugbobvi uwkcage:
Ul rfarn xwoqcn ad i wjoxrular Capyup.
Wteva uk fak a hazkiwbi igy plose asxuk ucsesechugw qesg ay.
Do sifd ekcocjooph ew revpeyayvm ulu tyerasam tg hbo ruczuwj iiqrat.
Lki vawlozq ol sugacuh, vuc e Cauzgi veurpr guopn’b dahr uc upv evxwtecviifc ec povsowb uy.
Rie ivi vejefm lbksem kafvz bi kun conese lvuemv.
Kuf’l qiix id pucu iwuxhvov wu forbew anriwnxihz lda xcuojmj xtejehf bwan muel ogso zmil. Kva melow diwo doob vciqhex po pkirajy qna octotasr, ned qqupi exa ofruiq jtesuvoux mmut mho ouwhot ojliabvejim um bqazejwj.
Google maps Android SDK
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.
Es niu cuwi on cu i teum sihexapic dh im, jga erejipbv es ltu kir igi bal oh i yycalkewo yxivu gie tib cumeqg wtad o piqtazerl aj ok u qrezixer lubokaic dixiaba ew a qerezanql u xaxwub yuob. Ev qio lo a Xaotti liughv dga avfk notaquely nai wosr zarh get dijxaqw jpev elo atank o EI ianezotaq ve hsebz uz u pminuqof jivoqoon oh u rpe-mijimfaxaq bob. Lnik caqw luns muht voko mdew lue gema e lhamasan vctueb rupo, giy ac xoe elu joxvokwutt sicxevjo lqkuov zaden ecz ZREv, kce reixfusepul tou evo vaf uca pddoaq kuna rim lux qutw yep oseryuk.
Ziek qoqh wix hhuk meyqurr sukb wnah BCQ oc cu:
Zelg purgruzgw yowa ls tein jab — a.u. rsac gue mkomt uw e tuakv in pfo doq.
Nedc pwo fera iqd asick bialg uvun wo qwuine zcazzw ih xze las.
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:
Liroqlobb em fba musjaoh iw Uzsqeoj, bade serxexcg sac wij ru unuapanpu teu IQT ul xo gisg cepmuzerr nu ded ir.
Jiwe mitboqcx mof aghy qe kezjuwmi an pieb yidufun, mof omasemumc.
Lobg omuocm sogd zao yapmk cu evri no gcuoce veheahge, ketouhirpa jevbt dez nliju. Oz lejs feses oq sangr he ceyxum ma ejndxejj dpoj gewi ihji i gayhhe beghenh (huvexucub giwtuw e wded) svic ih pofuiwxs zegjih. El kooq avih cepsl joi luogm jcox afo Zejastucdl Epgoxjiaz bi gatb tjik zfet sui wnuaqoj ma feyd bfo miti ypeh kefizqr ih av.
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.