27

So I'm trying to test my activity following googles instructions here: https://developer.android.com/guide/components/activities/testing

But the code launchActivity<MyActivity>() does not work. Do I need to define launchActivity as a rule or is there a library I need to import in gradle?

These are the imports I already have

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
Dan Anderson
  • 1,062
  • 13
  • 26
  • That should be a global or extension function in Kotlin that you would import. Android Studio should offer to import it for you if you have it in one of your dependencies. Which [AndroidX test dependencies](https://developer.android.com/training/testing/set-up-project#gradle-dependencies) are you using? – CommonsWare Mar 30 '19 at 15:49
  • I've added what test implementations I have in gradle. – Dan Anderson Mar 30 '19 at 15:59
  • 3
    Try adding `androidx.test:core-ktx:1.1.0`, as that is part of Android KTX, where Kotlin-specific stuff goes. Also, I recommend switching `runner` to `1.1.0` instead of `1.1.0-alpha4` and switching `espresso-core` to `3.1.1` instead of `3.1.0-alpha4`. https://dl.google.com/dl/android/maven2/index.html contains a list of all of the artifacts and their available versions. – CommonsWare Mar 30 '19 at 16:06

3 Answers3

24

You need to import following dependency into your gradle.

androidTestImplementation 'androidx.test:core-ktx:1.1.0'

Moreover, also add this in gradle file to avoid compile time error after adding launchActivity method in your test code.

kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }
NightFury
  • 13,436
  • 6
  • 71
  • 120
11

In Kotlin it seems that it needs to be like this for some reason:

val activityScenario = launch(MainActivity::class.java)

The import statement for this is:

import androidx.test.core.app.ActivityScenario.launch

Or maybe it's not the same thing? Don't know, but it works.

stackzebra
  • 450
  • 1
  • 6
  • 13
-2

Do I need to define launchActivity as a rule or is there a library I need to import in gradle?

Yes. You'll need to set up a rule at the beginning of your test class like this;

@RunWith(AndroidJUnit4.class)
public class YourActivityTests extends AndroidJUnitRunner {

    @Rule
    public ActivityTestRule<YourActivity> mYourActivityActivityTestRule =
        new ActivityTestRule<YourActivity>(YourActivity.class);

The required library imports are bundled in import androidx.test package.

This is for Java, but, there should be an equivalent way in Kotlin. Hope this helps.

Edit: You should use the latest stable builds (the ones with only numbers in versioning) for production apps. Only use any -alpha or -rc suffixed versions when you really need those versions of the library.

MD Naseem Ashraf
  • 1,030
  • 2
  • 9
  • 22
  • 2
    The question is asking about a different launchActivity() than the one you are referring to. You are referring to ActivityTestRule.launchActivity(), but the Google Dev link is using a function defined in androidx.test:core-ktx. – tronman Dec 23 '20 at 22:19