ir2pid
ir2pid

Reputation: 6126

Espresso test giving: No Koin Context configured. Please use startKoin or koinApplication DSL

I'm running a espresso uiautomator test which runs well when using the green run > button on android studio. (image below)

Yet ./gradlew connectedAndroidTest is giving an error:

No Koin Context configured. Please use startKoin or koinApplication DSL

Why does it run through android studio but not on gradle? And how do I fix it?

@LargeTest
@RunWith(AndroidJUnit4::class)
class MainActivityTest {
    @Rule
    @JvmField
    var mActivityTestRule = ActivityTestRule(MainActivity::class.java)

    lateinit var context: Context
    lateinit var mainActivity: MainActivity
    lateinit var idlingResource: MainActivityIdlingResource
    private lateinit var myDevice: UiDevice
    private val sleepMedium: Long = 1000

    @Before
    fun setup() {
        context = InstrumentationRegistry.getInstrumentation().targetContext
        mainActivity = mActivityTestRule.activity
        myDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
        idlingResource = MainActivityIdlingResource(
            mActivityTestRule.activity.recyclerList,
            mActivityTestRule.activity.javaClass.simpleName
        )
        IdlingRegistry.getInstance().register(idlingResource)
    }

    @After
    fun teardown() {
        IdlingRegistry.getInstance().unregister(idlingResource)
    }

    /**
     * check swipe
     */
    @Test
    fun testSwipe() {
        myDevice.findObject(UiSelector().descriptionContains("recyclerList"))
            .swipeUp(2) //to scroll up
        waitTime(sleepMedium)
        myDevice.findObject(UiSelector().descriptionContains("recyclerList"))
            .swipeDown(2) //to scroll down
        waitTime(sleepMedium)
    }

enter image description here

Upvotes: 6

Views: 818

Answers (2)

robigroza
robigroza

Reputation: 589

You are probably launching an activity that doesn't start Koin configuration.

If you for example have two activities, and one of them triggers Koin init, then if you skip one that inits Koin, you will receive an error like this.

Upvotes: 1

Sreeram Nair
Sreeram Nair

Reputation: 2387

You have to use startKoin and set the context by using androidContext for your Class MainActivityTest

startKoin {
                androidLogger()
                
                // declare used Android context
                androidContext(this@MainActivityTest)
                
                // declare modules
                modules(listOf(module1, module2, ...))
            }

Also, try checking that have you registered the Application class in manifest file

<application
    android:name=".MainActivityTest"

If this also doesn't work, Upgrade. The Start context has been fixed to be more consistent.

Upvotes: 3

Related Questions