0
votes

I think somethings wrong about my code in TeamImplsTest, and i need advice :D This is my code

API interface

interface API { @GET("lookupteam.php") fun getTeam(@Query("id") id: String): Call<TeamModel> }

TeamPresenter

interface MatchPresenter { fun loadTeamDetail(team_id: String) }

TeamImpls

class TeamImpls(val teamView: TeamView) : TeamPresenter {

    override fun loadTeamDetail(team_id: String) {
        val call = RetrofitConfig().getApi().getTeam(team_id)
        call.enqueue(object : Callback {
            override fun onResponse(call: Call, response: Response) {
                if (response.isSuccessful()) {
                    val res = response.body()
                    res?.let { teamView.onSuccess(it) }
                }
            }

            override fun onFailure(call: Call, t: Throwable) {
                Log.e("PrevMatchFragment", t.toString())
            }
        })
    }
}

TeamModel

data class TeamModel(
        val teams: ArrayList
)

data class TeamModeLResult(
        val idTeam: String,
        val strTeam: String,
        val strAlternate: String,
        val strSport: String,
        val strStadium: String,
        val strTeamBadge: String
)

And

This my TeamImplsTest

class TeamImplsTest {

    @Mock
    private lateinit var teamView: TeamView

    @Mock
    private lateinit var teamPresenter: TeamPresenter

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)

        teamPresenter = TeamImpls(teamView)
    }

    @Test
    fun loadTeamDetail() {
        val teams = TeamModel(arrayListOf())

        val teamId = "133613"

        teamPresenter.loadTeamDetail(teamId)

        Mockito.verify(teamView).onSuccess(teams)
    }
}

i got error

Wanted but not invoked:
teamView.onSuccess(TeamModel(teams=[]));
-> at com.fathurradhy.matchschedule.domain.presenter.TeamImplsTest.loadTeamDetail(TeamImplsTest.kt:34)
Actually, there were zero interactions with this mock.

Wanted but not invoked:
teamView.onSuccess(TeamModel(teams=[]));
-> at com.fathurradhy.matchschedule.domain.presenter.TeamImplsTest.loadTeamDetail(TeamImplsTest.kt:34)
Actually, there were zero interactions with this mock.
1
Can you provide more details about what exactly is the problem you are having?Poger
Can you please explain what is that is wrong? What are you experience? which errors, etcFred
I have changed my post..Fathur Radhy

1 Answers

3
votes

You're not mocking the API call as loadTeamDetail creates its own API instance.

To enable you to test the API call behaviour you could provide the API instance through your constructor, e.g.

class TeamImpls(private val api: API, private val teamView: TeamView) : TeamPresenter {

    override fun loadTeamDetail(team_id: String) {
        val call = api.getTeam(team_id)

This would then allow you to mock the api behaviour and verify the presenter calls the correct method when the call fails/succeeds, e.g.

class TeamImplsTest {

    @Mock
    private lateinit var teamView: TeamView

    @Mock
    private lateinit var api: API

    @Mock
    private lateinit var teamPresenter: TeamPresenter

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)

        teamPresenter = TeamImpls(api, teamView)
    }

    @Test
    fun loadTeamDetail() {
        val teams = TeamModel(arrayListOf())

        val teamId = "133613"

        // Use retrofit-mock to create your mockResponse.
        // See: https://github.com/square/retrofit/tree/master/retrofit-mock
        Mockito.`when`(api.getTeam(teamId)).thenReturn(Calls.response(teams)

        teamPresenter.loadTeamDetail(teamId)

        Mockito.verify(teamView).onSuccess(teams)
    }
}