Advertisement
uwong15

Unit Testing With Retrofit

Nov 11th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.33 KB | None | 0 0
  1. package com.app.rachmad.movie
  2. import com.app.rachmad.movie.webservice.AccessSite
  3. import okhttp3.OkHttpClient
  4. import org.junit.Before
  5. import org.junit.Test
  6. import org.junit.Assert.*
  7. import org.mockito.Mock
  8. import retrofit2.Retrofit
  9. import retrofit2.converter.gson.GsonConverterFactory
  10.  
  11.  
  12. class RemoteTest {
  13.  
  14.     @Mock
  15.     private lateinit var accessSite: AccessSite
  16.  
  17.     @Before
  18.     fun setup(){
  19.         accessSite = Retrofit.Builder()
  20.                 .baseUrl(BuildConfig.SERVER_URL)
  21.                 .addConverterFactory(GsonConverterFactory.create())
  22.                 .client(OkHttpClient())
  23.                 .build()
  24.                 .create(AccessSite::class.java)
  25. //        accessSite = MovieSite.connect()
  26.     }
  27.  
  28.     @Test
  29.     fun movieDataTest(){
  30.         val movie = accessSite.movieSite(1, "en-US").execute()
  31.         assertEquals(movie.body()!!.results!!.size, 20)
  32.  
  33.         movie.body()!!.results!!.forEach {
  34.             assertNotEquals(it.poster_path, null)
  35.             assertNotEquals(it.original_title, null)
  36.             assertNotEquals(it.overview, null)
  37.             assertNotEquals(it.release_date, null)
  38.             assertNotEquals(it.vote_average, null)
  39.         }
  40.     }
  41.  
  42.     @Test
  43.     fun movieDetailsDataTest(){
  44.         val movie = accessSite.movieDetail(429203, "en-US").execute()
  45.         with(movie.body()){
  46.             assertEquals(this!!.title, "The Old Man & the Gun")
  47.             assertEquals(this.backdrop_path, "/6X2YjjYcs8XyZRDmJAHNDlls7L4.jpg")
  48.             assertEquals(this.poster_path, "/a4BfxRK8dBgbQqbRxPs8kmLd8LG.jpg")
  49.             assertEquals(this.overview, "The true story of Forrest Tucker, from his audacious escape from San Quentin at the age of 70 to an unprecedented string of heists that confounded authorities and enchanted the public. Wrapped up in the pursuit are a detective, who becomes captivated with Forrest’s commitment to his craft, and a woman, who loves him in spite of his chosen profession.")
  50.             assertEquals(this.release_date, "2018-09-27")
  51.             this.genres.forEach {
  52.                 assertNotEquals(it.name, null)
  53.             }
  54.             assertEquals(this.vote_average, 6.3F)
  55.             this.production_companies.forEach {
  56.                 assertNotEquals(it.name, null)
  57. //                assertNotEquals(it.logo_path, null)
  58.             }
  59.         }
  60.     }
  61.  
  62.     @Test
  63.     fun tvDataTest(){
  64.         val movie = accessSite.tvSite(1, "en-US").execute()
  65.         assertEquals(movie.body()!!.results!!.size, 20)
  66.  
  67.         movie.body()!!.results!!.forEach {
  68.             assertNotEquals(it.name, null)
  69.             assertNotEquals(it.overview, null)
  70.             assertNotEquals(it.poster_path, null)
  71.             assertNotEquals(it.name, null)
  72.             assertNotEquals(it.overview, null)
  73.             assertNotEquals(it.first_air_date, null)
  74.             assertNotEquals(it.vote_average, null)
  75.         }
  76.     }
  77.  
  78.     @Test
  79.     fun tvDetailsDataTest(){
  80.         val tv = accessSite.tvDetail(60735, "en-US").execute()
  81.         with(tv.body()){
  82.             assertEquals(this!!.name, "The Flash")
  83.             assertEquals(this.backdrop_path, "/6ZdQTBy20HzWudZthAV7NkZWfIb.jpg")
  84.             assertEquals(this.poster_path, "/wHa6KOJAoNTFLFtp7wguUJKSnju.jpg")
  85.             assertEquals(this.overview, "After a particle accelerator causes a freak storm, CSI Investigator Barry Allen is struck by lightning and falls into a coma. Months later he awakens with the power of super speed, granting him the ability to move through Central City like an unseen guardian angel. Though initially excited by his newfound powers, Barry is shocked to discover he is not the only \"meta-human\" who was created in the wake of the accelerator explosion -- and not everyone is using their new powers for good. Barry partners with S.T.A.R. Labs and dedicates his life to protect the innocent. For now, only a few close friends and associates know that Barry is literally the fastest man alive, but it won't be long before the world learns what Barry Allen has become...The Flash.")
  86.             assertEquals(this.first_air_date, "2014-10-07")
  87.             this.genres.forEach {
  88.                 assertNotEquals(it.name, null)
  89.             }
  90.             assertEquals(this.vote_average, 6.7F)
  91.             this.production_companies.forEach {
  92.                 assertNotEquals(it.name, null)
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement