6

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

ir2pid
  • 5,604
  • 12
  • 63
  • 107
  • Have you tried updating your tests with the new androidx ActivityScenario? Deprecated code is always deprecated for good reasons. Also running the gradle task performs extra steps (like uninstalling the app at the end, while the green button never uninstalls it), maybe your app is being uninstalled and the data being cleared, thus impacting the execution of the app. – Raymond Arteaga Nov 19 '20 at 13:52

2 Answers2

3

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.

Sreeram Nair
  • 2,369
  • 12
  • 27
1

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.

robigroza
  • 559
  • 1
  • 5
  • 22