From fc674c54293df8a82081b53d8ccdb86a81884852 Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Sat, 20 Jun 2020 14:59:45 -0300 Subject: [PATCH] Try fix tests --- app/build.gradle | 8 +++++-- .../dev/lucasnlm/antimine/TestApplication.kt | 12 ---------- .../lucasnlm/antimine/di/TestLevelModule.kt | 7 +++--- .../antimine/level/view/LevelFragmentTest.kt | 15 +++++++++++-- app/src/test/resources/robolectric.properties | 2 ++ .../common/level/di/TestLevelModule.kt | 22 ++++++++----------- 6 files changed, 34 insertions(+), 32 deletions(-) delete mode 100644 app/src/test/java/dev/lucasnlm/antimine/TestApplication.kt create mode 100644 app/src/test/resources/robolectric.properties diff --git a/app/build.gradle b/app/build.gradle index 79f19e9a..79fc5444 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,8 +1,8 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' -apply plugin: 'kotlin-kapt' apply plugin: 'dagger.hilt.android.plugin' +apply plugin: 'kotlin-kapt' android { compileSdkVersion 29 @@ -100,13 +100,17 @@ dependencies { testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.5' // Tests - testImplementation 'junit:junit:4.12' + testImplementation 'junit:junit:4.13' + testImplementation 'androidx.core:core:1.1.0' testImplementation 'androidx.test:core:1.2.0' testImplementation 'androidx.test:core-ktx:1.2.0' + testImplementation 'androidx.test:rules:1.2.0' + testImplementation 'androidx.test:runner:1.2.0' testImplementation 'androidx.test.espresso:espresso-core:3.2.0' testImplementation 'androidx.test.espresso:espresso-contrib:3.2.0' testImplementation 'androidx.fragment:fragment-testing:1.1.0' testImplementation 'org.robolectric:robolectric:4.3.1' + testImplementation 'androidx.test.ext:junit:1.1.1' } tasks.withType(Test) { diff --git a/app/src/test/java/dev/lucasnlm/antimine/TestApplication.kt b/app/src/test/java/dev/lucasnlm/antimine/TestApplication.kt deleted file mode 100644 index ff746804..00000000 --- a/app/src/test/java/dev/lucasnlm/antimine/TestApplication.kt +++ /dev/null @@ -1,12 +0,0 @@ -package dev.lucasnlm.antimine - -import android.app.Application -import dev.lucasnlm.antimine.di.AppModule -import dev.lucasnlm.antimine.di.TestLevelModule - -class TestApplication : MainApplication() { - - override fun appModule(application: Application) = AppModule(application) - - override fun levelModule(application: Application) = TestLevelModule(application) -} diff --git a/app/src/test/java/dev/lucasnlm/antimine/di/TestLevelModule.kt b/app/src/test/java/dev/lucasnlm/antimine/di/TestLevelModule.kt index e0ae07a4..dc3cd18b 100644 --- a/app/src/test/java/dev/lucasnlm/antimine/di/TestLevelModule.kt +++ b/app/src/test/java/dev/lucasnlm/antimine/di/TestLevelModule.kt @@ -1,11 +1,12 @@ package dev.lucasnlm.antimine.di -import android.app.Application +import android.content.Context import androidx.lifecycle.MutableLiveData import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.android.components.ActivityComponent +import dagger.hilt.android.qualifiers.ApplicationContext import dev.lucasnlm.antimine.common.level.models.Event import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository import dev.lucasnlm.antimine.common.level.repository.IMinefieldRepository @@ -33,7 +34,7 @@ class TestLevelModule { @Provides fun provideGameViewModelFactory( - application: Application, + @ApplicationContext context: Context, eventObserver: MutableLiveData, savesRepository: ISavesRepository, statsRepository: IStatsRepository, @@ -44,7 +45,7 @@ class TestLevelModule { analyticsManager: AnalyticsManager, clock: Clock ) = GameViewModelFactory( - application, + context, eventObserver, savesRepository, statsRepository, diff --git a/app/src/test/java/dev/lucasnlm/antimine/level/view/LevelFragmentTest.kt b/app/src/test/java/dev/lucasnlm/antimine/level/view/LevelFragmentTest.kt index 8fa0454d..86e3945b 100644 --- a/app/src/test/java/dev/lucasnlm/antimine/level/view/LevelFragmentTest.kt +++ b/app/src/test/java/dev/lucasnlm/antimine/level/view/LevelFragmentTest.kt @@ -1,12 +1,18 @@ package dev.lucasnlm.antimine.level.view +import android.os.Build import androidx.recyclerview.widget.RecyclerView import androidx.test.core.app.launchActivity +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 dev.lucasnlm.antimine.GameActivity import dev.lucasnlm.antimine.R -import dev.lucasnlm.antimine.TestApplication +import dev.lucasnlm.antimine.common.level.di.LevelModule import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull +import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner @@ -15,10 +21,15 @@ import org.robolectric.annotation.LooperMode import org.robolectric.shadows.ShadowLooper import java.util.concurrent.TimeUnit +@HiltAndroidTest +@UninstallModules(LevelModule::class) @RunWith(RobolectricTestRunner::class) -@Config(sdk = [16, 19, 21, 26, 27], application = TestApplication::class) +@Config(sdk = [Build.VERSION_CODES.P], application = HiltTestApplication::class) @LooperMode(LooperMode.Mode.PAUSED) class LevelFragmentTest { + @get:Rule + var rule = HiltAndroidRule(this) + @Test fun testShowGameOverWhenTapAMine() { launchActivity().onActivity { activity -> diff --git a/app/src/test/resources/robolectric.properties b/app/src/test/resources/robolectric.properties new file mode 100644 index 00000000..1a7a2e6e --- /dev/null +++ b/app/src/test/resources/robolectric.properties @@ -0,0 +1,2 @@ +sdk=28 +application=dagger.hilt.android.testing.HiltTestApplication diff --git a/common/src/test/java/dev/lucasnlm/antimine/common/level/di/TestLevelModule.kt b/common/src/test/java/dev/lucasnlm/antimine/common/level/di/TestLevelModule.kt index dcb93d48..7c1a2602 100644 --- a/common/src/test/java/dev/lucasnlm/antimine/common/level/di/TestLevelModule.kt +++ b/common/src/test/java/dev/lucasnlm/antimine/common/level/di/TestLevelModule.kt @@ -5,6 +5,9 @@ import android.content.Context import androidx.lifecycle.MutableLiveData import dagger.Module import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.components.ApplicationComponent +import dagger.hilt.android.qualifiers.ApplicationContext import dev.lucasnlm.antimine.common.level.mocks.FixedDimensionRepository import dev.lucasnlm.antimine.common.level.models.Event import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository @@ -22,9 +25,8 @@ import dev.lucasnlm.antimine.core.analytics.AnalyticsManager import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository @Module -class TestLevelModule( - private val application: Application -) { +@InstallIn(ApplicationComponent::class) +class TestLevelModule { @Provides fun provideGameEventObserver(): MutableLiveData = MutableLiveData() @@ -33,7 +35,7 @@ class TestLevelModule( @Provides fun provideGameViewModelFactory( - application: Application, + @ApplicationContext context: Context, eventObserver: MutableLiveData, savesRepository: ISavesRepository, statsRepository: IStatsRepository, @@ -44,7 +46,7 @@ class TestLevelModule( analyticsManager: AnalyticsManager, clock: Clock ) = GameViewModelFactory( - application, + context, eventObserver, savesRepository, statsRepository, @@ -57,10 +59,7 @@ class TestLevelModule( ) @Provides - fun provideDimensionRepository( - context: Context, - preferencesRepository: IPreferencesRepository - ): IDimensionRepository = FixedDimensionRepository() + fun provideDimensionRepository(): IDimensionRepository = FixedDimensionRepository() @Provides fun provideSavesRepository(): ISavesRepository = MemorySavesRepository() @@ -72,8 +71,5 @@ class TestLevelModule( fun provideStatsRepository(): IStatsRepository = MemoryStatsRepository() @Provides - fun provideHapticFeedbackInteractor( - application: Application, - preferencesRepository: IPreferencesRepository - ): IHapticFeedbackInteractor = DisabledIHapticFeedbackInteractor() + fun provideHapticFeedbackInteractor(): IHapticFeedbackInteractor = DisabledIHapticFeedbackInteractor() }