From 69b2d504934eab4e69b7ef9a574c6c7560cee54b Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 15 Jun 2020 20:11:12 -0700 Subject: Update AS and add hilt usage --- .../ca/allanwang/kau/sample/KPrefViewTest.kt | 20 +++++--- .../ca/allanwang/kau/sample/SampleTestApp.kt | 56 ++++++++-------------- .../kotlin/ca/allanwang/kau/sample/TestModules.kt | 15 ++++++ 3 files changed, 49 insertions(+), 42 deletions(-) create mode 100644 sample/src/androidTest/kotlin/ca/allanwang/kau/sample/TestModules.kt (limited to 'sample/src/androidTest') diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt index 88a0402..91c98e7 100644 --- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt @@ -27,6 +27,10 @@ import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.MediumTest import androidx.test.rule.ActivityTestRule +import dagger.hilt.android.testing.HiltAndroidRule +import dagger.hilt.android.testing.HiltAndroidTest +import dagger.hilt.android.testing.HiltTestApplication +import dagger.hilt.android.testing.UninstallModules import kotlin.test.BeforeTest import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -40,8 +44,7 @@ import org.junit.Test import org.junit.rules.RuleChain import org.junit.rules.TestRule import org.junit.runner.RunWith -import org.koin.test.KoinTest -import org.koin.test.inject +import javax.inject.Inject /** * Created by Allan Wang on 21/12/2018. @@ -49,19 +52,22 @@ import org.koin.test.inject * Tests related to the :kpref-activity module */ @RunWith(AndroidJUnit4::class) -@MediumTest -class KPrefViewTest : KoinTest { +@HiltAndroidTest +@UninstallModules(PrefFactoryModule::class) +class KPrefViewTest { val activity: ActivityTestRule = ActivityTestRule(MainActivity::class.java) + val hiltRule = HiltAndroidRule(this) + @get:Rule - val rule: TestRule = RuleChain.outerRule(SampleTestRule()).around(activity) + val rule: TestRule = RuleChain.outerRule(hiltRule).around(SampleTestRule()).around(activity) - private val pref: KPrefSample by inject() + @Inject lateinit var pref: KPrefSample @BeforeTest fun before() { - pref.reset() + hiltRule.inject() } fun verifyCheck(checked: Boolean): Matcher { diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt index c6ab259..da53ce1 100644 --- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt @@ -17,18 +17,17 @@ package ca.allanwang.kau.sample import android.app.Application import android.content.Context +import androidx.test.core.app.ApplicationProvider import androidx.test.runner.AndroidJUnitRunner -import ca.allanwang.kau.kpref.KPrefFactory -import ca.allanwang.kau.kpref.KPrefFactoryInMemory +import dagger.hilt.EntryPoint +import dagger.hilt.InstallIn +import dagger.hilt.android.EntryPointAccessors +import dagger.hilt.android.components.ApplicationComponent +import dagger.hilt.android.testing.HiltTestApplication import org.junit.rules.TestRule import org.junit.runner.Description import org.junit.runners.model.Statement -import org.koin.android.ext.koin.androidContext -import org.koin.android.ext.koin.androidLogger -import org.koin.core.KoinComponent -import org.koin.core.context.startKoin -import org.koin.core.get -import org.koin.dsl.module +import java.lang.RuntimeException class SampleTestRunner : AndroidJUnitRunner() { override fun newApplication( @@ -36,43 +35,30 @@ class SampleTestRunner : AndroidJUnitRunner() { className: String?, context: Context? ): Application { - return super.newApplication(cl, SampleTestApp::class.java.name, context) + return super.newApplication(cl, HiltTestApplication::class.java.name, context) } } class SampleTestRule : TestRule { + + @EntryPoint + @InstallIn(ApplicationComponent::class) + interface SampleTestRuleEntryPoint { + fun pref(): KPrefSample + } + override fun apply(base: Statement, description: Description): Statement = - object : Statement(), KoinComponent { + object : Statement() { override fun evaluate() { + val entryPoint = EntryPointAccessors.fromApplication( + ApplicationProvider.getApplicationContext(), + SampleTestRuleEntryPoint::class.java + ) // Reset prefs - get().reset() + entryPoint.pref().reset() base.evaluate() } } } - -class SampleTestApp : Application() { - override fun onCreate() { - super.onCreate() - startKoin { - if (BuildConfig.DEBUG) { - androidLogger() - } - androidContext(this@SampleTestApp) - modules( - listOf( - prefFactoryModule(), - KPrefSample.module() - ) - ) - } - } - - fun prefFactoryModule() = module { - single { - KPrefFactoryInMemory - } - } -} diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/TestModules.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/TestModules.kt new file mode 100644 index 0000000..061af50 --- /dev/null +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/TestModules.kt @@ -0,0 +1,15 @@ +package ca.allanwang.kau.sample + +import ca.allanwang.kau.kpref.KPrefFactory +import ca.allanwang.kau.kpref.KPrefFactoryInMemory +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.components.ApplicationComponent + +@Module +@InstallIn(ApplicationComponent::class) +object PrefFactoryTestModule { + @Provides + fun factory(): KPrefFactory = KPrefFactoryInMemory +} \ No newline at end of file -- cgit v1.2.3