Advertisement
iskhak

LeaguesViewModelTest2.kt

Dec 7th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.26 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.MockitoAnnotations
  14. import retrofit2.Call
  15. import retrofit2.Callback
  16. import retrofit2.Response
  17.  
  18. class LeaguesViewModelTest {
  19.  
  20.     @Mock
  21.     private val leagusListData = MutableLiveData<ArrayList<LeaguesDetailModel>>()
  22.     @Mock
  23.     private val listItems = ArrayList<LeaguesDetailModel>()
  24.  
  25.     @Before
  26.     fun setUp(){
  27.         MockitoAnnotations.initMocks(this)
  28.     }
  29.  
  30.     @Test
  31.     fun setLeaguesList() {
  32.         ApiService().getApi().getLeagues().enqueue(object : Callback<LeaguesResponse> {
  33.             override fun onFailure(call: Call<LeaguesResponse>, t: Throwable) {
  34.                 Log.i("FAILED GET ", "LIST OF LEAGUES ERROR : ${t} ")
  35.             }
  36.  
  37.             override fun onResponse(
  38.                 call: Call<LeaguesResponse>,
  39.                 response: Response<LeaguesResponse>
  40.             ) {
  41.                 response.body()?.leagues?.let {
  42.                    getLeageuBadge()
  43.                 }
  44.                 leagusListData.postValue(listItems)
  45.             }
  46.  
  47.         })
  48.     }
  49.  
  50.  
  51.     @Test
  52.     fun getLeageuBadge() {
  53.  
  54.         ApiService().getApi().getLeaguesById("4346").enqueue(object : Callback<LeagueDetailResponse> {
  55.             override fun onFailure(call: Call<LeagueDetailResponse>, t: Throwable) {
  56.                 Log.i("FAILED GET ", "LEAGUE DETAIL ERROR : ${t} ")
  57.             }
  58.  
  59.             override fun onResponse(
  60.                 call: Call<LeagueDetailResponse>,
  61.                 response: Response<LeagueDetailResponse>
  62.             ) {
  63.                 response.body()?.leagues?.let {
  64.                     for (i in it) {
  65.                         if (i?.strBadge != null) {
  66.                             Log.i("${i.strLeague} ", " ${i.strBadge}")
  67.                             listItems.add(i)
  68.                         }
  69.                     }
  70.                 }
  71.             }
  72.  
  73.         })
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement