All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricSandboxManager.kt Maven / Gradle / Ivy

Go to download

This repository aims to bridge the gap between JUnit 5 and Robolectric, enabling developers to leverage the benefits of both frameworks for unit testing Android applications. While Robolectric currently lacks a dedicated JUnit 5 extension, this project proposes a community-driven solution to achieve seamless integration.

The newest version!
package tech.apter.junit.jupiter.robolectric.internal

import org.robolectric.annotation.GraphicsMode
import org.robolectric.annotation.LooperMode
import org.robolectric.annotation.ResourcesMode
import org.robolectric.annotation.SQLiteMode
import org.robolectric.internal.AndroidSandbox
import org.robolectric.internal.SandboxManager
import org.robolectric.internal.bytecode.InstrumentationConfiguration
import org.robolectric.pluginapi.Sdk
import org.robolectric.plugins.SdkCollection
import tech.apter.junit.jupiter.robolectric.internal.extensions.outerMostDeclaringClass
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject

internal class JUnit5RobolectricSandboxManager @Inject constructor(
    private val testClassContainer: TestClassContainer,
    private val sandboxBuilder: SandboxBuilder,
    private val sdkCollection: SdkCollection,
) : SandboxManager(sandboxBuilder, sdkCollection) {

    @Synchronized
    override fun getAndroidSandbox(
        instrumentationConfig: InstrumentationConfiguration,
        sdk: Sdk,
        resourcesMode: ResourcesMode.Mode,
        looperMode: LooperMode.Mode,
        sqliteMode: SQLiteMode.Mode,
        graphicsMode: GraphicsMode.Mode,
    ): AndroidSandbox {
        val testClass = testClassContainer.testClass
        val compileSdk = sdkCollection.maxSupportedSdk

        val key = SandboxKey(
            testClassName = testClass.outerMostDeclaringClass().name,
            instrumentationConfiguration = instrumentationConfig,
            sdk = sdk,
            resourcesMode = resourcesMode,
            graphicsMode = graphicsMode,
        )
        // Return the same sandbox for nested tests
        return sandboxCache.getOrPut(key) {
            sandboxBuilder.build(instrumentationConfig, sdk, compileSdk, resourcesMode, sqliteMode)
        }.apply {
            updateModes(sqliteMode)
        }
    }

    private data class SandboxKey(
        private val testClassName: String,
        private val instrumentationConfiguration: InstrumentationConfiguration,
        private val sdk: Sdk,
        private val resourcesMode: ResourcesMode.Mode,
        private val graphicsMode: GraphicsMode.Mode,
    )

    private companion object {
        private val sandboxCache = ConcurrentHashMap()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy