From 7a46597b3e5504479b631e220ccc4b885f8ffca7 Mon Sep 17 00:00:00 2001 From: Russell Wolf Date: Fri, 2 Feb 2018 10:18:52 -0500 Subject: [PATCH] Add test case for BasePresenter --- .../skotlinton/base/BasePresenterTest.kt | 74 +++++++++++++++++++ .../screens/example1/Example1PresenterTest.kt | 4 +- .../screens/example2/Example2PresenterTest.kt | 4 +- ...ePresenterTest.kt => PresenterTestBase.kt} | 2 +- 4 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 app/src/test/kotlin/io/intrepid/skotlinton/base/BasePresenterTest.kt rename app/src/test/kotlin/io/intrepid/skotlinton/testutils/{BasePresenterTest.kt => PresenterTestBase.kt} (95%) diff --git a/app/src/test/kotlin/io/intrepid/skotlinton/base/BasePresenterTest.kt b/app/src/test/kotlin/io/intrepid/skotlinton/base/BasePresenterTest.kt new file mode 100644 index 0000000..93418be --- /dev/null +++ b/app/src/test/kotlin/io/intrepid/skotlinton/base/BasePresenterTest.kt @@ -0,0 +1,74 @@ +package io.intrepid.skeleton.base + +import io.intrepid.skotlinton.base.BaseContract +import io.intrepid.skotlinton.base.BasePresenter +import io.intrepid.skotlinton.base.PresenterConfiguration +import io.intrepid.skotlinton.testutils.PresenterTestBase +import io.reactivex.Observable +import io.reactivex.disposables.Disposable +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.mockito.Mock +import org.mockito.Mockito.* + +class BasePresenterTest : PresenterTestBase>() { + @Mock + private lateinit var view: BaseContract.View + + @Mock + private lateinit var onBind: Runnable + + @Mock + private lateinit var onUnbind: Runnable + + private lateinit var disposable: Disposable + + @Before + fun setUp() { + presenter = TestPresenter(view, testConfiguration) + } + + @Test + fun bind_onlyOnce() { + presenter.bindView(view) + presenter.bindView(view) + verify(onBind, times(1)).run() + } + + @Test + fun unbind_onlyOnce() { + presenter.unbindView() + verify(onUnbind, never()).run() + + presenter.bindView(view) + presenter.unbindView() + presenter.unbindView() + verify(onUnbind, times(1)).run() + } + + @Test + fun unbind_clearsDisposables() { + presenter.bindView(view) + assertFalse(disposable.isDisposed) + + presenter.unbindView() + assertTrue(disposable.isDisposed) + } + + private inner class TestPresenter(view: BaseContract.View, configuration: PresenterConfiguration) : + BasePresenter(view, configuration) { + + override fun onViewBound() { + onBind.run() + disposable = Observable.never().subscribe() + disposables.add(disposable) + } + + override fun onViewUnbound() { + onUnbind.run() + } + } + +} diff --git a/app/src/test/kotlin/io/intrepid/skotlinton/screens/example1/Example1PresenterTest.kt b/app/src/test/kotlin/io/intrepid/skotlinton/screens/example1/Example1PresenterTest.kt index 380b2a8..9e294c1 100644 --- a/app/src/test/kotlin/io/intrepid/skotlinton/screens/example1/Example1PresenterTest.kt +++ b/app/src/test/kotlin/io/intrepid/skotlinton/screens/example1/Example1PresenterTest.kt @@ -1,12 +1,12 @@ package io.intrepid.skotlinton.screens.example1 -import io.intrepid.skotlinton.testutils.BasePresenterTest +import io.intrepid.skotlinton.testutils.PresenterTestBase import org.junit.Before import org.junit.Test import org.mockito.Mock import org.mockito.Mockito.verify -internal class Example1PresenterTest : BasePresenterTest() { +internal class Example1PresenterTest : PresenterTestBase() { @Mock lateinit var mockView: Example1Contract.View diff --git a/app/src/test/kotlin/io/intrepid/skotlinton/screens/example2/Example2PresenterTest.kt b/app/src/test/kotlin/io/intrepid/skotlinton/screens/example2/Example2PresenterTest.kt index a111d11..47df697 100644 --- a/app/src/test/kotlin/io/intrepid/skotlinton/screens/example2/Example2PresenterTest.kt +++ b/app/src/test/kotlin/io/intrepid/skotlinton/screens/example2/Example2PresenterTest.kt @@ -3,13 +3,13 @@ package io.intrepid.skotlinton.screens.example2 import com.nhaarman.mockito_kotlin.verify import com.nhaarman.mockito_kotlin.whenever import io.intrepid.skotlinton.models.IpModel -import io.intrepid.skotlinton.testutils.BasePresenterTest +import io.intrepid.skotlinton.testutils.PresenterTestBase import io.reactivex.Single import org.junit.Before import org.junit.Test import org.mockito.Mock -internal class Example2PresenterTest : BasePresenterTest() { +internal class Example2PresenterTest : PresenterTestBase() { @Mock lateinit var mockView: Example2Contract.View diff --git a/app/src/test/kotlin/io/intrepid/skotlinton/testutils/BasePresenterTest.kt b/app/src/test/kotlin/io/intrepid/skotlinton/testutils/PresenterTestBase.kt similarity index 95% rename from app/src/test/kotlin/io/intrepid/skotlinton/testutils/BasePresenterTest.kt rename to app/src/test/kotlin/io/intrepid/skotlinton/testutils/PresenterTestBase.kt index 9fc97cc..a113b82 100644 --- a/app/src/test/kotlin/io/intrepid/skotlinton/testutils/BasePresenterTest.kt +++ b/app/src/test/kotlin/io/intrepid/skotlinton/testutils/PresenterTestBase.kt @@ -11,7 +11,7 @@ import org.junit.Rule import org.mockito.junit.MockitoJUnit import org.mockito.junit.MockitoRule -abstract class BasePresenterTest

> { +abstract class PresenterTestBase

> { @Rule @JvmField val mockitoRule: MockitoRule = MockitoJUnit.rule()