Skip to content

Instantly share code, notes, and snippets.

@kingargyle
Created February 19, 2019 13:56
Show Gist options
  • Save kingargyle/e73e861a4d3d22e9f657e5a6c53f99c0 to your computer and use it in GitHub Desktop.
Save kingargyle/e73e861a4d3d22e9f657e5a6c53f99c0 to your computer and use it in GitHub Desktop.
A bare bones RobolectrictTestRunner that only starts up a sandbox and does not attempt to load any resources. This is good for non-ui tests where you still need portions of the android library but not any of the resources. Reduces robolectric startup time to 2 seconds. This comes from the following google group post: https://groups.google.com/fo…
package us.nineworlds.serenity.testrunner
import org.junit.runners.model.FrameworkMethod
import org.junit.runners.model.InitializationError
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.internal.SandboxTestRunner
import org.robolectric.internal.bytecode.Sandbox
import java.lang.reflect.Method
class PlainAndroidRunner @Throws(InitializationError::class)
constructor(testClass: Class<*>) : RobolectricTestRunner(testClass) {
override fun afterTest(method: FrameworkMethod, bootstrappedMethod: Method?) {}
@Throws(Throwable::class)
override fun beforeTest(sandbox: Sandbox, method: FrameworkMethod, bootstrappedMethod: Method?) {
}
override fun getHelperTestRunner(bootstrappedTestClass: Class<*>): org.robolectric.internal.SandboxTestRunner.HelperTestRunner {
try {
return SandboxTestRunner.HelperTestRunner(bootstrappedTestClass)
} catch (initializationError: InitializationError) {
throw RuntimeException(initializationError)
}
}
override fun buildGlobalConfig(): Config {
return Config.Builder().setManifest(Config.NONE).build()
}
}
@kingargyle
Copy link
Author

Note this is under an MIT license.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment