I am trying to write a simple test for my MainPresenter(SchedulerProvider) class. It should check if the showEventFragment(String) and showPromptFragment(String) shows up after successful API response.
I create MainPresenter instance in MainActivity like this:
private val presenter: MainContract.Presenter = MainPresenter(AppSchedulerProvider())
Presenter class:
class MainPresenter(private val scheduler: SchedulerProvider) : MainContract.Presenter{
private val subscriptions = CompositeDisposable()
private val api: RxApiServiceInterface = RxApiServiceInterface.create()
private lateinit var view: MainContract.View
override fun subscribe() {
}
override fun unsubscribe() {
subscriptions.clear()
}
override fun loadProfileData() {
view.showLoadingView()
val subscribe = api.getProfileDataRequest()
.subscribeOn(scheduler.io())
.observeOn(scheduler.ui())
.subscribe({profileList : List<Profile> ->
view.showEventFragment(profileList[0].profile_id)
view.showPromptFragment(profileList[0].profile_id)
},{ error ->
view.showErrorMessage(error.message.toString())
view.showRetryView()
})
subscriptions.add(subscribe)
}
override fun attach(view: MainContract.View) {
this.view = view
}
}
Test class:
class MainPresenterTest {
@Mock
private lateinit var view: MainContract.View
private var api: RxApiServiceInterface = RxApiServiceInterface.create()
private lateinit var mainPresenter: MainPresenter
private lateinit var testScheduler: TestScheduler
private lateinit var testSchedulerProvider: TestSchedulerProvider
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
testScheduler = TestScheduler()
testSchedulerProvider = TestSchedulerProvider(testScheduler)
mainPresenter = MainPresenter(testSchedulerProvider)
mainPresenter.attach(view)
}
@Test
fun getProfileSuccess() {
val address = Address("SillyStreet", "4", "80333", "NY")
val contact = Contact("i.am@dummy.com", "000 000 000 00")
val mockedProfile = Profile(
"freemium_profile",
"Dum",
"my",
"male",
"Sil ly",
true,
"First Class",
"1989-01-01",
address,
contact,
listOf("freemium", "signup_complete")
)
doReturn(Single.just(listOf(mockedProfile)))
.`when`(api)
.getProfileDataRequest()
mainPresenter.loadProfileData()
testSchedulerProvider.testScheduler.triggerActions()
verify(view).showLoadingView()
verify(view).showEventFragment(mockedProfile.profile_id)
verify(view).showPromptFragment(mockedProfile.profile_id)
}
}
RxApiServiceInterface interface:
interface RxApiServiceInterface {
@GET("user/customer/profiles")
fun getProfileDataRequest() : Single<List<Profile>>
companion object {
private val restClient by lazy {
RestClient.createRetrofit(API_URL)
}
fun create(): RxApiServiceInterface = restClient.create(RxApiServiceInterface::class.java)
}
}
TestSchedulerProvider class:
class TestSchedulerProvider constructor(val testScheduler: TestScheduler) : SchedulerProvider {
override fun ui(): Scheduler = testScheduler
override fun computation(): Scheduler = testScheduler
override fun io(): Scheduler = testScheduler
}
I am using these test libs:
testImplementation 'org.mockito:mockito-core:2.22.0'
testImplementation 'org.mockito:mockito-inline:2.22.0'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit:1.2.71'
androidTestImplementation 'org.mockito:mockito-android:2.7.22'
What the hell am I doing wrong, that I still get the "Wanted but not invoked error"? The logs also says:
However, there was exactly 1 interaction with this mock:
view.showLoadingView();
but I am not suprised about that as this method is outside API query.