Try fix tests

This commit is contained in:
Lucas Lima 2020-06-20 14:59:45 -03:00
parent 38d518a686
commit fc674c5429
No known key found for this signature in database
GPG key ID: 0259A3F43EC1027A
6 changed files with 34 additions and 32 deletions

View file

@ -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) {

View file

@ -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)
}

View file

@ -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<Event>,
savesRepository: ISavesRepository,
statsRepository: IStatsRepository,
@ -44,7 +45,7 @@ class TestLevelModule {
analyticsManager: AnalyticsManager,
clock: Clock
) = GameViewModelFactory(
application,
context,
eventObserver,
savesRepository,
statsRepository,

View file

@ -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<GameActivity>().onActivity { activity ->

View file

@ -0,0 +1,2 @@
sdk=28
application=dagger.hilt.android.testing.HiltTestApplication

View file

@ -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<Event> = MutableLiveData()
@ -33,7 +35,7 @@ class TestLevelModule(
@Provides
fun provideGameViewModelFactory(
application: Application,
@ApplicationContext context: Context,
eventObserver: MutableLiveData<Event>,
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()
}