main.tech.apter.junit.jupiter.robolectric.internal.RobolectricTestClassClassLoader.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of robolectric-extension Show documentation
Show all versions of robolectric-extension Show documentation
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.junit.platform.commons.logging.Logger
import tech.apter.junit.jupiter.robolectric.internal.extensions.createLogger
import tech.apter.junit.jupiter.robolectric.internal.extensions.isExtendedWithRobolectric
import java.io.InputStream
import java.net.URL
import java.util.Enumeration
/**
* This ClassLoader is a proxy to load classes with the correct Robolectric ClassLoader.
* If a test class is not annotated with @ExtendWith(RobolectricExtension::class) then use the `parentClassLoader`.
* Only used to load the test classes.
*/
internal class RobolectricTestClassClassLoader(
private val parentClassLoader: ClassLoader,
private val robolectricClassLoaderFactory: (testClass: Class<*>) -> ClassLoader,
) : ClassLoader() {
private inline val logger: Logger get() = createLogger()
override fun loadClass(name: String): Class<*> {
val clazz = parentClassLoader.loadClass(name)
val extendedWithRobolectric = clazz.isExtendedWithRobolectric()
logger.trace {
"Load ${clazz.name.substringAfterLast(
'.'
)} class with ${if (extendedWithRobolectric) "Robolectric" else "app"} classLoader"
}
return if (extendedWithRobolectric) {
robolectricClassLoaderFactory(clazz).loadClass(name)
} else {
clazz
}
}
override fun getResource(name: String): URL? {
logger.trace { "getResource($name)" }
return parentClassLoader.getResource(name)
}
override fun getResourceAsStream(name: String): InputStream? {
logger.trace { "getResourceAsStream($name)" }
return parentClassLoader.getResourceAsStream(name)
}
override fun getResources(name: String): Enumeration {
logger.trace { "getResources($name)" }
return parentClassLoader.getResources(name)
}
}