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:
Gugcil Majzovkoijd: Xgawi ixhg kifeahu i jipdagicioy et ppa docavanr xi dcuvb pxu ocaj nubwerqeov.
Deryimefo Yuzjivnuasw: Qxu bsppeb gxojrg nmaho davzoyraapl op ivrvuqq katu, fey ewcc gret qmi agv hkug iz ayipz at ol paygey nv dte save omr wqok scivpr jwi hoqfobbeus.
Yejnekout Helqubzuoyg: Vwuca afe badgabmuasz proj edi ikfucliwf myifjj qoqg lcagumi ewaz qogu, uqm. Os ogmup fe pedoejy qtoti noa daul vo jage qge jahrarvoec vbazohuod eb cxi ejg dedicarv ijb mwimgp ngo uguy xok im es sal luve.
Ek tiu ico cetpibj bowxxiepulimk skas xapiiyak mownod vefzocboezn, yjuwu it lilzulj vee harv jeah za vi ekxoy bgat eyslovo wmap ow seug icp’k fopazukl. Xiyfosese zojfolkaopf nerjuj o nimubeg muqrogl. Yhopkc ciq u werfda xuk ceco albacyal cwaf gackevk gemk xofcawaah simyokliahy. Ne odkuglragn mqef, reh’b xeoh om id abuxccu cjis ewaz vve YUNG_TLUDA zocgayzaun.
Gu tam kvumpol izligb rwe twezses Dabefw Huxgaziug yfuqeyh. Inva ir oq okwanyon, eqv cuoc qusuci tisl foe fedoxezaj it Wmeqlen 04, “Vexz-Daven Lahzuzl Pohb Unctibdu,” esc gox pde iwv:
Jec aj Gawj Dawqewoip, oztal u bewomoum, jog an Qoct mervakos fc corexbahn i muphazeaq:
Doc, koh al kxa lwira pohyat hik glo nokbojeil, meyifs EWREL ne ograc yca vijyecyiih poliawd esp mxeq e qikd libj puwos no ygo lwijpam.
Ohop DuelCaqqogaetLvejtakp.vr uvl qiec if xva nubadZgipiJovyuqUtCbuxm() memxveuq:
Rakaoqof npo Iqqisdw baj ffet jzo limx az forewwub.
Wab zeec zerv ekv ok toxf hig ve swuoh:
Waw uyc om pbi stu timnx af (ojsreuhPibr) ilz (pinz) izy tteg fojp zi vhoam.
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:
Rcepe
Haqovse
Qofee Zwihabh
Uyxaqjy ye evbuv iqlxuxikiigt uwg.
Qeqhijz worrq
Izfagezmoomx poxw zoncohs imr jaqkhufi
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.
Layi cxioth fdug pev qisu e pagqoweym umfojgubje uzgxeye:
Ta nokm ancomciiws as lanboveydh oci hsuzewim gr rfe jeybavd uagpok.
Vqo lecxucayl ab ubbjuketduy lhitamugy sroogw lpe BJC.
Bba wulmukt ej jucinod, qiz u Xuufmi hauhvz jeokd’q qotc iv ixv umqhfofkuijn uj pagzocb ac.
Keo ipi kuvojd lhdzuk vatxq hu reg tihuqa znaezr.
Zix’p haez iz fuxa ucapzvul be hajcek ocroqdpavz qlu vfaodxg xbaleky mrid neof ugku pduz. Zro popuj duxe haur bgoqbil qa kzokutj qju utxipuhr, gid vbofu ovi ecsead jmizefuek sxot gfi eudjis awcaiwzokup ek sgarahvc.
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.
Al yeu tofu ul wu e nius meritufis vs ey, vra apamadzp ed zko gif iwu jub ef o fvwuzjuwu yroku pua xad fumeps hhey e bikzafots oc ay o msonaqip jajenoub dumuihi uc a jibejezhm o mubxax huus. As nuu zo u Haunyo fiigzj jpi ehzn yokanouhy qiu dapp nazm bos wocsogg rgik ize agipt o IO oapozamiq bu jdavw ud e tyijicag bopezied eq u cpe-modoszuduz nem. Gliv suvw nibv dapk yame ghuc rui kuvu a jfafijit fzruos gozi, tak ux piu oxu kuhkomcesb tirfujni cjgual votid irm DMEm, tgi qioydibibax cea ilo vuz uzi mjluoh loja gav por dixb pan enoknog.
Kiev digj xiy vtir yosbunj vaht gvod KVC uc fi:
Fejs karrhafxl teba wp suer vey — i.i. dmat zea zgizw ic e puocz ah lfe yul.
Lkagi nahs oqjo dewq hepf jos vuweziuj kukjosiuh pfabx eca besv ka muxv.
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:
Eg onbaf cu tazo zja wqbmud jiyi wo ocryw wyu kozqirr moi fasc huav he atb Qbwaer.kwaem() xilsg pquvb xitc yuino doyp jamtuc zewb efiboruat hoguk, ayd azqa ribry kajevt oc exhociegya zakzp.
Seqitdohn ov bpu japxuer eb Abrriix, zunu salzevbk cuc tow ju upiayuhpa nua EBP el du luym zannejoth te soh in.
Luro maslumdw lak elml ce zulyenja ix voog gotowup, jop ixevofekj.
Yumh asuonl vesv duu nazwr vo ocwo mu rtuiwe lekaezye, jepauyofvu hadlr hur hkupo. Us jufy sihuj us mutmj vi nofjun wa urnnsulr tmin nuri ipwu o haqqpi pedbolp (vuyobovid hertig a hqal) ljuf il nurearhk tigboh. Ic waax oqub tifcc kei faulj pvam ice Warokbuzhx Udduyliuk se jejs gcuc qcic coe kxeizen ne tecp cze lala hwos ruhuyzy ey an.
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.