Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
Share definition of CoroutineExecutorExtension

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.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If you're using Gradle, the test fixtures plugin is an appropriate solution. The plugin is built in, so you can simply add java-test-fixtures to your plugins block.

plugins {
    `java-test-fixtures`
}

This automatically creates a new testFixtures source set, alongside the existing main and test source sets that you already have. Code that you write inside src/testFixtures/kotlin will be visible from your tests, and will have access to everything in main. Anything that the test fixtures need (for example, dependencies on kotlinx-coroutines or your test framework) should be added as dependencies of the test fixtures.

You can also also add dependencies on test fixtures from another module. For example, if module-b needs to use the test fixtures from module-a, then in the Gradle build script for module-b you could add:

dependencies {
    testImplementation(testFixtures(project(":module-a")))
}

The examples I've given are for Kotlin build scripts, but you can find the Groovy equivalents in the linked documentation.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda