Advertisement
iskhak

LeaguesViewModelTest.kt

Dec 7th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.40 KB | None | 0 0
  1. package com.example.myleague.apitesting
  2.  
  3. import android.util.Log
  4. import androidx.lifecycle.MutableLiveData
  5. import com.example.myleague.model.league.LeagueDetailResponse
  6. import com.example.myleague.model.league.LeaguesDetailModel
  7. import com.example.myleague.model.league.LeaguesResponse
  8. import com.example.myleague.network.ApiService
  9. import org.junit.Before
  10.  
  11. import org.junit.Test
  12. import org.mockito.Mock
  13. import org.mockito.Mockito
  14. import org.mockito.MockitoAnnotations
  15. import retrofit2.Call
  16. import retrofit2.Callback
  17. import retrofit2.Response
  18.  
  19. class LeaguesViewModelTest {
  20.  
  21.     @Mock
  22.     private val leagusListData = MutableLiveData<ArrayList<LeaguesDetailModel>>()
  23.     @Mock
  24.     private val listItems = ArrayList<LeaguesDetailModel>()
  25.  
  26.     @Before
  27.     fun setUp(){
  28.         MockitoAnnotations.initMocks(this)
  29.         Mockito.`when`(setLeaguesList()).thenReturn(getData())
  30.     }
  31.  
  32.     @Test
  33.     fun setLeaguesList() {
  34.         ApiService().getApi().getLeagues().enqueue(object : Callback<LeaguesResponse> {
  35.             override fun onFailure(call: Call<LeaguesResponse>, t: Throwable) {
  36.                 Log.i("FAILED GET ", "LIST OF LEAGUES ERROR : ${t} ")
  37.             }
  38.  
  39.             override fun onResponse(
  40.                 call: Call<LeaguesResponse>,
  41.                 response: Response<LeaguesResponse>
  42.             ) {
  43.                 response.body()?.leagues?.let {
  44.                    getLeageuBadge()
  45.                 }
  46.                 leagusListData.postValue(listItems)
  47.             }
  48.  
  49.         })
  50.     }
  51.  
  52.  
  53.     @Test
  54.     fun getLeageuBadge() {
  55.  
  56.         ApiService().getApi().getLeaguesById("4346").enqueue(object : Callback<LeagueDetailResponse> {
  57.             override fun onFailure(call: Call<LeagueDetailResponse>, t: Throwable) {
  58.                 Log.i("FAILED GET ", "LEAGUE DETAIL ERROR : ${t} ")
  59.             }
  60.  
  61.             override fun onResponse(
  62.                 call: Call<LeagueDetailResponse>,
  63.                 response: Response<LeagueDetailResponse>
  64.             ) {
  65.                 response.body()?.leagues?.let {
  66.                     for (i in it) {
  67.                         if (i?.strBadge != null) {
  68.                             Log.i("${i.strLeague} ", " ${i.strBadge}")
  69.                             listItems.add(i)
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.  
  75.         })
  76.     }
  77.  
  78.     fun getData() : Unit{
  79.         leagusListData
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement