From 581ede03e295ec39f5bf880de16791461f3d0d24 Mon Sep 17 00:00:00 2001 From: miladsi90 Date: Thu, 25 Jul 2019 13:18:44 +0430 Subject: [PATCH] Create base test and work on user repository test , issue #21 --- app/build.gradle | 6 ++ .../de/netalic/peacock/ExampleUnitTest.kt | 16 ----- .../java/de/netalic/peacock/base/BaseTest.kt | 45 ++++++++++++++ .../data/repository/UserRepositoryTest.kt | 62 +++++++++++++++++++ 4 files changed, 113 insertions(+), 16 deletions(-) delete mode 100644 app/src/test/java/de/netalic/peacock/ExampleUnitTest.kt create mode 100644 app/src/test/java/de/netalic/peacock/base/BaseTest.kt create mode 100644 app/src/test/java/de/netalic/peacock/data/repository/UserRepositoryTest.kt diff --git a/app/build.gradle b/app/build.gradle index 9ff5ada..b1c9cc3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -81,4 +81,10 @@ dependencies { exclude group: 'androidx.appcompat', module: 'appcompat' } implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0' + + //test + testImplementation 'junit:junit:4.12' + testImplementation "org.mockito:mockito-inline:3.0.0" + testImplementation 'org.koin:koin-test:2.0.1' + testImplementation "android.arch.core:core-testing:1.1.1" } diff --git a/app/src/test/java/de/netalic/peacock/ExampleUnitTest.kt b/app/src/test/java/de/netalic/peacock/ExampleUnitTest.kt deleted file mode 100644 index 5f29af3..0000000 --- a/app/src/test/java/de/netalic/peacock/ExampleUnitTest.kt +++ /dev/null @@ -1,16 +0,0 @@ -package de.netalic.peacock - -import org.junit.Assert.assertEquals -import org.junit.Test - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } -} diff --git a/app/src/test/java/de/netalic/peacock/base/BaseTest.kt b/app/src/test/java/de/netalic/peacock/base/BaseTest.kt new file mode 100644 index 0000000..974191a --- /dev/null +++ b/app/src/test/java/de/netalic/peacock/base/BaseTest.kt @@ -0,0 +1,45 @@ +package de.netalic.peacock.base + +import io.reactivex.Scheduler +import io.reactivex.android.plugins.RxAndroidPlugins +import io.reactivex.disposables.Disposable +import io.reactivex.internal.schedulers.ExecutorScheduler +import io.reactivex.plugins.RxJavaPlugins +import org.junit.AfterClass +import org.junit.BeforeClass +import java.util.concurrent.Executor +import java.util.concurrent.TimeUnit + +open class BaseTest { + + + companion object { + + @JvmStatic + @BeforeClass + fun setUpClass() { + + val immediate = object : Scheduler() { + override fun scheduleDirect(run: Runnable, delay: Long, unit: TimeUnit): Disposable { + return super.scheduleDirect(run, 0, unit) + } + + override fun createWorker(): Worker { + return ExecutorScheduler.ExecutorWorker(Executor { it.run() }) + } + } + RxJavaPlugins.setInitIoSchedulerHandler { scheduler -> immediate } + RxJavaPlugins.setInitComputationSchedulerHandler { scheduler -> immediate } + RxJavaPlugins.setInitNewThreadSchedulerHandler { scheduler -> immediate } + RxJavaPlugins.setInitSingleSchedulerHandler { scheduler -> immediate } + RxAndroidPlugins.setInitMainThreadSchedulerHandler { scheduler -> immediate } + } + + @JvmStatic + @AfterClass + fun tearDownClass() { + RxJavaPlugins.reset() + RxAndroidPlugins.reset() + } + } +} \ No newline at end of file diff --git a/app/src/test/java/de/netalic/peacock/data/repository/UserRepositoryTest.kt b/app/src/test/java/de/netalic/peacock/data/repository/UserRepositoryTest.kt new file mode 100644 index 0000000..3b8a4cd --- /dev/null +++ b/app/src/test/java/de/netalic/peacock/data/repository/UserRepositoryTest.kt @@ -0,0 +1,62 @@ +package de.netalic.peacock.data.repository + +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import de.netalic.peacock.base.BaseTest +import de.netalic.peacock.data.model.User +import de.netalic.peacock.data.webservice.InterfaceApi +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.MockitoAnnotations + + +class UserRepositoryTest: BaseTest() { + + + @get:Rule + val instantExecutorRule=InstantTaskExecutorRule() + + companion object{ + + val sUser=User( + "+989211499302", + "D89707AC55BAED9E8F23B826FB2A28E96095A190", + "salimi", + "android", + "eyJ0eXAiOiAiSldUIiwgImFsZyI6ICJSUzI1NiIsICJraWQiOiAiODk0NTkyNzQzMzlkMzNlZmNmNTE3MDc4NGM5Z" + + "GU1MjUzMjEyOWVmZiJ9.eyJpc3MiOiAiZmlyZWJhc2UtYWRtaW5zZGstaXp1MTNAYWxwaGEtZDY0ZTQuaWFtLmdzZXJ2aWNlYWN" + + "jb3VudC5jb20iLCAic3ViIjogImZpcmViYXNlLWFkbWluc2RrLWl6dTEzQGFscGhhLWQ2NGU0LmlhbS5nc2VydmljZWFjY291bn" + + "QuY29tIiwgImF1ZCI6ICJodHRwczovL2lkZW50aXR5dG9vbGtpdC5nb29nbGVhcGlzLmNvbS9nb29nbGUuaWRlbnRpdHkuaWRlb" + + "nRpdHl0b29sa2l0LnYxLklkZW50aXR5VG9vbGtpdCIsICJ1aWQiOiAiKzk4OTIxMTQ5OTMwMiIsICJpYXQiOiAxNTYzOTYwNDU3" + + "LCAiZXhwIjogMTU2Mzk2NDA1N30.HOUVBzwbmGwsglQHukGwrijlUuSZ241KdN2Eol3Gy80mmd4Kxoc58m3VhL71AWv3WS99eE7" + + "uz6xctl--yLPilhN3WJ_z2nxySqkhxiZ9OtaH_U8sTek63SJgfINeTFzJFpWHkT_DlQNPTVoH_AqbXjh0gZwdpVdMyoLmmuJf-W" + + "Iqx2y7BdwudCTiAqY_RoK7DdDwS8Jf28J-czpWi7Q4neUo1pC0WLi986u9n0mZcfIhWoVB_fV0A2-fWRV6yhT647sfHntC2eSg-" + + "OJZKO-MAyBsgKDIZm_ubX7m3LHD6rahpnUHtY8m33eJyD-EfZcKboRWalJkmje69abirvep1A", + "082016" + ) + } + + private lateinit var mUserRepository: UserRepository + + @Mock + private lateinit var mInterfaceApi:InterfaceApi + + @Before + fun setUp(){ + + MockitoAnnotations.initMocks(this) + mUserRepository=UserRepository(mInterfaceApi) + } + + @Test + fun bindUser_bindToApi(){ + + mUserRepository.bind(sUser) + Mockito.verify(mInterfaceApi).bind(sUser.phone, sUser.udid, sUser.deviceName, sUser.deviceType, + sUser.firebaseRegistrationId, sUser.actionCode) + + } + +} \ No newline at end of file