Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda