Skip to content

Commit

Permalink
Improve kotest robolectric config injection (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
uOOOO authored Jun 24, 2024
1 parent 9f1c337 commit fe085b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ import org.junit.runners.model.FrameworkMethod
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.internal.bytecode.InstrumentationConfiguration
import org.robolectric.pluginapi.config.ConfigurationStrategy
import org.robolectric.pluginapi.config.Configurer
import org.robolectric.plugins.HierarchicalConfigurationStrategy
import org.robolectric.util.inject.Injector
import java.lang.reflect.Method

@RunWith(Enclosed::class)
internal class ContainedRobolectricRunner(
private val config: Config
) : RobolectricTestRunner(PlaceholderTest::class.java, injector) {
init {
injectKotestConfig()
}
config: Config
) : RobolectricTestRunner(PlaceholderTest::class.java, kotestInjector(config)) {

private val placeHolderMethod: FrameworkMethod = children[0]
val sdkEnvironment = getSandbox(placeHolderMethod).also {
Expand All @@ -43,16 +40,6 @@ internal class ContainedRobolectricRunner(
.build()
}

private fun injectKotestConfig() {
val configurationStrategyField =
RobolectricTestRunner::class.java
.getField(ConfigurationStrategy::class.java)
.apply { isAccessible = true }
val configurers = injector.getInstance(arrayOf<Configurer<*>>()::class.java)
val newConfigurationStrategy = KotestHierarchicalConfigurationStrategy(config, configurers)
configurationStrategyField.set(this, newConfigurationStrategy)
}

class PlaceholderTest {
@org.junit.Test
fun testPlaceholder() {
Expand All @@ -76,6 +63,10 @@ internal class ContainedRobolectricRunner(
}

companion object {
private val injector = defaultInjector().build()
private fun kotestInjector(config: Config): Injector {
val defaultInjector = defaultInjector().bind(Config::class.java, config).build()
return Injector.Builder(defaultInjector, ContainedRobolectricRunner::class.java.classLoader)
.build()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
br.com.colman.kotest.android.extensions.robolectric.ContainedRobolectricRunner$KotestHierarchicalConfigurationStrategy

0 comments on commit fe085b5

Please sign in to comment.