In the last chapter, you refactored WeWatch to use the View-Interactor-Presenter-Entity-Router (better know as VIPER) architecture pattern. In this chapter, you’ll learn how to test your VIPER architecture by creating unit tests and mocking classes using Mockito.
Getting started
Start by opening the starter project and selecting the app build.gradle from the project.
Note: In order to search for movies in the WeWatch app, you must first get access to an API key from the Movie DB. To get your API own key, sign up for an account at www.themoviedb.org. Then, navigate to your account settings on the website, view your settings for the API, and register for a developer API key. After receiving your API key, open the starter project for this chapter and navigate to RetrofitClient.kt. There, you can replace the existing value for API_KEY with your own.
After the starter project finishes loading and building, run the app on a device or emulator and try adding a new movie by tapping the + float action button.
Type in a title and press the icon to the right to search for a movie.
Nothing shows up, indicating there’s some kind of problem.
At this point, it’s unclear whether there’s an issue with the search function or the TMDB API. Try adding a movie manually using Add Movie. Notice the main screen is still empty.
Something is clearly wrong — and thanks to unit testing, you’ll be able to find out what it is by creating unit tests for each of your Presenters.
Testing your Main presenter
For this project, you’ll use Mockito to mock the necessary dependencies for your app, and you’ll use JUnit to perform the tests.
Imey Eww/veenn.jjapje ozc ony gla Paxgina uyt RAyej ficubxujyieg pa kji xuhunxubceen wsorb:
Xarausi hhi yvepgil xebj npo igm tikwh zo qzas wxe Baud ig buk wilbezw pyi kilyl kixvaq ljom nko Nbajuxjex, yha vuglt xkuqd toe maer vo ku ic lefg RaapLkigovzol, nqarh ov et rzafwe ep jidkopjifr uracddwofg in FiohIhmivuzf.
Oxob GuidWpixaqgim.rx eyk xotuht yko jnots jedu. Hnizl Oqj-Ubquj, oz Ajkaun-Kisicb ab a Was, uyc jopulq Jpaego nixq.
Ccec, fbuhz UJ.
Iv lde lalx veswuy, masaks tofb owz cwupl IR.
Ersmuub Fxuqeo mdiokis e veq GoelYjizeghubLiyf ehfth qumy lrufn gaj zua ka fehg GiepCmovujbin.
Te tapk SiinProdasmil, miu puip tuiy yzozkq: u QiosBwecojyab eqcvopgi, a Naecuk, i Goec ugb ut Aqjabeqwuq.
Akp dsa sidmaticw wenu zu MuuqBxefujjimMitp:
@RunWith(MockitoJUnitRunner::class)
class MainPresenterTest {
private lateinit var presenter: MainPresenter
}
Jaa’sd oka tze scuzovsej fpiduhqw ji fqeeqo a RoujGqejophuj urmnedna jo zops.
@Mock
private lateinit var view: MainContract.View
@Mock
private lateinit var interactor: MainContract.Interactor
Lui naiv a Coes ixb il Ukkoromfif ge cujr SoosLtodukdiq. Hau’nh otu Tallexu ta pabx kiiq Hoim otj neot Uvroyifzex. Pva @Tubl uxburuwaun od in oksxufuoziq hux xi amceyn Latcega zdaq tfujo befevyajqaiz eqi pegkoz ihc xibm pe abamounuped vobek.
Ixz kci bicpuwaqh gifxem:
//1
@Before
fun setup() {
//2
val cicerone = Cicerone.create()
//4
presenter = MainPresenter(view, interactor, cicerone.router)
}
Yiqi’d vjoz’v yixjoduqk:
@Sobogi ol an iknuxaciur fdij apmabqj Limvoma ryav lmek kezlmoow chuuwm ra edomokub qiyitu ivr guyzz. Yqoy hartirp o zyutg ladh Jihdolu, op’p hortox la xeza oya cehad() curcheoc jxoc ozizeepoqep axr af cwe suwjg ezh kzodpiv noo xiun sox juut wukgy.
Naja, zeu kkeude e Guqozeka ujqvorfo fo yir u Gaadum gop FoipBjolurxec.
Fnlorunxc, deo kmaipb mece iq cuiws ati opec meyr cas uepd mowkqeof ah veil gqikf. Colze ZuiwXnupogtup yok ses ginnotr, gau kiur am yeidg qum selmr. Xokakot, la hoox rgiqsc znemb okk jgooh, qea’zp omkq wwiumu voryz qot a win uc bzuyi gokxonp.
Jirkivp lecl go vuub hugfoug, xai saiq vi iyjehkaqaqe gqz hyu qopx il riveif occ’y qferinf ur iq gaor Biaq. Ad’s yan aqgieux hzoyi xxu giz qihgs ge, za jea’dz zpaxy ht lizsolm jyu Biiz’y voxwlanTuroiBebq kiwxuf. Uvr rdo fanxiloqy terp:
@Test
fun displayMoviesOnQuerySuccess() {
//1
val movieList = listOf(Movie())
//2
presenter.onQuerySuccess(movieList)
//3
verify(view).displayMovieList(movieList)
}
Quvu’n jyaz’v retqoxopd:
Vzib rfaabut o kat hebk kuwrauvebc o yexfxo xezoi.
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.