Skip to content

Commit a42872f

Browse files
authored
Fix not working Robolectric tests with kotest-extensions-android dependency (#30)
1 parent 06a78c5 commit a42872f

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

kotest-extensions-android/kotest-extensions-android-tests/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ dependencies {
4141
testImplementation(project(":kotest-extensions-android"))
4242
testImplementation("io.kotest:kotest-runner-junit5:5.9.1")
4343
testImplementation("org.robolectric:robolectric:4.12.2")
44+
testImplementation("androidx.test.espresso:espresso-core:3.6.1")
45+
testImplementation("org.junit.vintage:junit-vintage-engine:5.11.3")
4446
androidTestImplementation("androidx.test:runner:1.5.2")
4547
androidTestImplementation("androidx.test:core:1.5.0")
4648
androidTestImplementation("androidx.test:rules:1.5.0")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package br.com.colman.kotest.android.extensions
2+
3+
import androidx.test.core.app.ActivityScenario.launch
4+
import androidx.test.espresso.Espresso.onView
5+
import androidx.test.espresso.assertion.ViewAssertions.matches
6+
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
7+
import androidx.test.espresso.matcher.ViewMatchers.withId
8+
import androidx.test.espresso.matcher.ViewMatchers.withText
9+
import br.com.colman.kotest.R
10+
import br.com.colman.kotest.TestActivity
11+
import org.junit.Test
12+
import org.junit.runner.RunWith
13+
import org.robolectric.RobolectricTestRunner
14+
15+
@RunWith(RobolectricTestRunner::class)
16+
class NoKotestRobolectricTest {
17+
@Test
18+
fun `TextView in TestActivity has Hello World!`() {
19+
launch(TestActivity::class.java)
20+
21+
onView(withId(R.id.textView))
22+
.check(matches(isDisplayed()))
23+
.check(matches(withText("Hello World!")))
24+
}
25+
}

kotest-extensions-android/src/main/kotlin/br/com/colman/kotest/android/extensions/robolectric/ContainedRobolectricRunner.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.junit.runners.model.FrameworkMethod
66
import org.robolectric.RobolectricTestRunner
77
import org.robolectric.annotation.Config
88
import org.robolectric.internal.bytecode.InstrumentationConfiguration
9+
import org.robolectric.pluginapi.config.ConfigurationStrategy
910
import org.robolectric.pluginapi.config.Configurer
1011
import org.robolectric.plugins.HierarchicalConfigurationStrategy
1112
import org.robolectric.util.inject.Injector
@@ -65,7 +66,10 @@ internal class ContainedRobolectricRunner(
6566

6667
companion object {
6768
private fun kotestInjector(config: Config): Injector {
68-
val defaultInjector = defaultInjector().bind(Config::class.java, config).build()
69+
val defaultInjector = defaultInjector()
70+
.bind(Config::class.java, config)
71+
.bind(ConfigurationStrategy::class.java, KotestHierarchicalConfigurationStrategy::class.java)
72+
.build()
6973
return Injector.Builder(defaultInjector, ContainedRobolectricRunner::class.java.classLoader)
7074
.build()
7175
}

kotest-extensions-android/src/main/resources/META-INF/services/org.robolectric.pluginapi.config.ConfigurationStrategy

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)