Convert to my app, so I can continue a fork!
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.atridad.mealient.test
|
||||
|
||||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
||||
import com.atridad.mealient.architecture.configuration.AppDispatchers
|
||||
import com.atridad.mealient.logging.Logger
|
||||
import io.mockk.MockKAnnotations
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.rules.Timeout
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
open class BaseUnitTest {
|
||||
|
||||
@get:Rule(order = 0)
|
||||
val instantExecutorRule = InstantTaskExecutorRule()
|
||||
|
||||
@get:Rule(order = 1)
|
||||
val timeoutRule: Timeout = Timeout.seconds(20)
|
||||
|
||||
protected val logger: Logger = FakeLogger()
|
||||
|
||||
lateinit var dispatchers: AppDispatchers
|
||||
|
||||
@Before
|
||||
open fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
Dispatchers.setMain(UnconfinedTestDispatcher())
|
||||
dispatchers = object : AppDispatchers {
|
||||
override val io: CoroutineDispatcher = UnconfinedTestDispatcher()
|
||||
override val main: CoroutineDispatcher = UnconfinedTestDispatcher()
|
||||
override val default: CoroutineDispatcher = UnconfinedTestDispatcher()
|
||||
override val unconfined: CoroutineDispatcher = UnconfinedTestDispatcher()
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
Dispatchers.resetMain()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.atridad.mealient.test
|
||||
|
||||
import com.atridad.mealient.logging.Logger
|
||||
import com.atridad.mealient.logging.MessageSupplier
|
||||
import javax.inject.Inject
|
||||
|
||||
class FakeLogger @Inject constructor() : Logger {
|
||||
override fun v(throwable: Throwable?, tag: String?, messageSupplier: MessageSupplier) {
|
||||
print("V", throwable, messageSupplier)
|
||||
}
|
||||
|
||||
override fun d(throwable: Throwable?, tag: String?, messageSupplier: MessageSupplier) {
|
||||
print("D", throwable, messageSupplier)
|
||||
}
|
||||
|
||||
override fun i(throwable: Throwable?, tag: String?, messageSupplier: MessageSupplier) {
|
||||
print("I", throwable, messageSupplier)
|
||||
}
|
||||
|
||||
override fun w(throwable: Throwable?, tag: String?, messageSupplier: MessageSupplier) {
|
||||
print("W", throwable, messageSupplier)
|
||||
}
|
||||
|
||||
override fun e(throwable: Throwable?, tag: String?, messageSupplier: MessageSupplier) {
|
||||
print("E", throwable, messageSupplier)
|
||||
}
|
||||
|
||||
private fun print(
|
||||
level: String,
|
||||
throwable: Throwable?,
|
||||
messageSupplier: MessageSupplier,
|
||||
) {
|
||||
println("$level ${messageSupplier()}. ${throwable?.stackTraceToString().orEmpty()}")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.atridad.mealient.test
|
||||
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import dagger.hilt.testing.TestInstallIn
|
||||
import com.atridad.mealient.logging.Logger
|
||||
import com.atridad.mealient.logging.LoggerModule
|
||||
|
||||
@Module
|
||||
@TestInstallIn(
|
||||
components = [SingletonComponent::class],
|
||||
replaces = [LoggerModule::class]
|
||||
)
|
||||
interface FakeLoggerModule {
|
||||
|
||||
@Binds
|
||||
fun bindFakeLogger(impl: FakeLogger): Logger
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.atridad.mealient.test
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import dagger.hilt.android.testing.HiltAndroidRule
|
||||
import dagger.hilt.android.testing.HiltTestApplication
|
||||
import com.atridad.mealient.logging.Logger
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.annotation.Config
|
||||
import javax.inject.Inject
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
@Config(application = HiltTestApplication::class, manifest = Config.NONE, sdk = [Config.NEWEST_SDK])
|
||||
abstract class HiltRobolectricTest {
|
||||
|
||||
@get:Rule
|
||||
var hiltRule = HiltAndroidRule(this)
|
||||
|
||||
@Inject
|
||||
lateinit var logger: Logger
|
||||
|
||||
@Before
|
||||
fun inject() {
|
||||
hiltRule.inject()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user