Skip to content

Commit

Permalink
Prepare tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fidloo committed Mar 24, 2021
1 parent b49bd2b commit eca3717
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 48 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,8 @@ dependencies {
androidTestImplementation Libs.AndroidX.Test.rules
androidTestImplementation Libs.AndroidX.Test.Ext.junit
androidTestImplementation Libs.AndroidX.Compose.uiTest
androidTestImplementation Libs.AndroidX.Test.runner
androidTestImplementation Libs.Coroutines.test
androidTestImplementation Libs.Hilt.library
androidTestImplementation Libs.Hilt.testing
}
41 changes: 41 additions & 0 deletions app/src/androidTest/java/com/fidloo/flux/CoroutinesTestRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fidloo.flux

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description

@OptIn(ExperimentalCoroutinesApi::class)
class CoroutinesTestRule constructor(
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
) : TestWatcher() {

override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(testDispatcher)
}

override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
testDispatcher.cleanupTestCoroutines()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 The Android Open Source Project
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,11 +15,13 @@
*/
package com.fidloo.flux

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
// Add unit tests here
import android.app.Application
import android.content.Context
import androidx.test.runner.AndroidJUnitRunner
import dagger.hilt.android.testing.HiltTestApplication

class CustomTestRunner : AndroidJUnitRunner() {
override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("DEPRECATION")

package com.fidloo.flux.di

import com.fidloo.flux.domain.di.DefaultDispatcher
import com.fidloo.flux.domain.di.DispatchersModule
import dagger.Module
import dagger.Provides
import dagger.hilt.components.SingletonComponent
import dagger.hilt.testing.TestInstallIn
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi

@OptIn(ExperimentalCoroutinesApi::class)
@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [DispatchersModule::class]
)
class TestDispatchersModule {

@Provides
@DefaultDispatcher
fun provideDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Unconfined
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ object Libs {

object Test {
private const val version = "1.3.0"
const val runner = "androidx.test:runner:$version"
const val core = "androidx.test:core:$version"
const val rules = "androidx.test:rules:$version"

Expand Down
7 changes: 7 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}

packagingOptions {
// Multiple dependency bring these files in. Exclude them to enable
// our test APK to build (has no effect on our AARs)
excludes += "/META-INF/AL2.0"
excludes += "/META-INF/LGPL2.1"
}
}

dependencies {
Expand Down
7 changes: 7 additions & 0 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}

packagingOptions {
// Multiple dependency bring these files in. Exclude them to enable
// our test APK to build (has no effect on our AARs)
excludes += "/META-INF/AL2.0"
excludes += "/META-INF/LGPL2.1"
}
}

dependencies {
Expand Down
7 changes: 7 additions & 0 deletions domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}

packagingOptions {
// Multiple dependency bring these files in. Exclude them to enable
// our test APK to build (has no effect on our AARs)
excludes += "/META-INF/AL2.0"
excludes += "/META-INF/LGPL2.1"
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import kotlinx.coroutines.Dispatchers

@Module
@InstallIn(SingletonComponent::class)
object CoroutinesModule {
object DispatchersModule {

@DefaultDispatcher
@JvmStatic
Expand Down
11 changes: 11 additions & 0 deletions presentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ android {
kotlinCompilerExtensionVersion compose_version
}

packagingOptions {
// Multiple dependency bring these files in. Exclude them to enable
// our test APK to build (has no effect on our AARs)
excludes += "/META-INF/AL2.0"
excludes += "/META-INF/LGPL2.1"
}

}

dependencies {
Expand Down Expand Up @@ -85,4 +92,8 @@ dependencies {
androidTestImplementation Libs.AndroidX.Test.rules
androidTestImplementation Libs.AndroidX.Test.Ext.junit
androidTestImplementation Libs.AndroidX.Compose.uiTest
androidTestImplementation Libs.AndroidX.Test.runner
androidTestImplementation Libs.Coroutines.test
androidTestImplementation Libs.Hilt.library
androidTestImplementation Libs.Hilt.testing
}

0 comments on commit eca3717

Please sign in to comment.