0

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.
3
  • Can you provide more details about what exactly is the problem you are having? Commented Sep 6, 2018 at 6:09
  • Can you please explain what is that is wrong? What are you experience? which errors, etc Commented Sep 6, 2018 at 6:20
  • I have changed my post.. Commented Sep 6, 2018 at 6:33

1 Answer 1

3

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)
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry before, could u help me to check my github github.com/radhsyn83/match_schedule Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.