I have created two classes:
@ExperimentalCoroutinesApi
open class InstantExecutorExtension : BeforeEachCallback, AfterEachCallback {
override fun beforeEach(context: ExtensionContext?) {
ArchTaskExecutor.getInstance()
.setDelegate(object : TaskExecutor() {
override fun executeOnDiskIO(runnable: Runnable) = runnable.run()
override fun postToMainThread(runnable: Runnable) = runnable.run()
override fun isMainThread(): Boolean = true
})
}
override fun afterEach(context: ExtensionContext?) {
ArchTaskExecutor.getInstance().setDelegate(null)
}
}
and
@ExperimentalCoroutinesApi
class CoroutineExecutorExtension : InstantExecutorExtension() {
private val testCoroutineDispatcher = TestCoroutineDispatcher()
override fun beforeEach(context: ExtensionContext?) {
super.beforeEach(context)
Dispatchers.setMain(testCoroutineDispatcher)
}
override fun afterEach(context: ExtensionContext?) {
super.afterEach(context)
Dispatchers.resetMain()
testCoroutineDispatcher.cleanupTestCoroutines()
}
}
I'm using these classes to write unit tests for method that use coroutines and livedata.
The problem I have is that I don't know how to share these classes in different modules.
If I define them in the test folder then I can build the classes but they are not visible in other modules.
If I define in the main folder there are compile time errors.
What can I do to avoid having to define these classes in all the modules?
UPDATE To follow @Sam suggestion I have created a module that contains the two classes in the folder:
.\unittest\src\testFixtures\java\com\name\terminal\unittest
The build.gradle file of this module uses the plugins:
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'java-test-fixtures'
}
In the module where I want to use the CoroutineExecutorExtension I have imported the project of the unittest module with:
testImplementation(testFixtures(project(":unittest")))
The problem is that the classes in the testFixtures folder are not visiible.
EDIT
This method does not work for an Android library. I can't use a java library because I need to be able to include Androidx dependencies.