25

I'm following the dev guide here:

https://developer.android.com/guide/components/activities/testing

and have a test class like:

@RunWith(AndroidJUnit4::class)
class MyTestSuite {
   @get:Rule var activityScenarioRule = activityScenarioRule<MyActivity>()

   @Test fun testEvent() {
    val scenario = activityScenarioRule.scenario
   }
}

the method activityScenarioRule<T>() is not defined. What dependency do I need? Also, what is the best way to determine which dependencies to add when reading these docs?

Stephen__T
  • 2,004
  • 3
  • 25
  • 48

4 Answers4

42

The activityScenarioRule<T>() method is part of the androidx.test.ext:junit-ktx:1.1.0 dependency.

Usually, this would be listed under the List of AndroidX Test dependencies, but it appears it isn't up to date with the junit-ktx or core-ktx modules as of yet, despite it being explicitly mentioned as part of the Version 1.1.0-beta01 release notes

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Do I need to add a static import to reference it? – Stephen__T Feb 26 '19 at 05:04
  • You'll need to add an import, yes. But it isn't a static import. – ianhanniballake Feb 26 '19 at 05:07
  • Hmm, still not seeing it. It does look like `launchActivity` is now available though. – Stephen__T Feb 26 '19 at 05:08
  • Ah, yeah, looks like `activityScenarioRule` is part of the `androidx.test.ext:junit-ktx` module, not the `core-ktx` module (which has the `launchActivity` equivalent). – ianhanniballake Feb 26 '19 at 05:29
  • The `activityScenarioRule` causes my build to fail for: `Cannot inline bytecode built with JVM target 1.7 into bytecode that is being built with JVM target 1.6. Please specify proper '-jvm-target' option` – Stephen__T Mar 03 '19 at 19:35
  • 1
    @ZeroDivide - sounds like you should [compile with Java 8](https://developer.android.com/studio/write/java8-support) and [file a bug](https://issuetracker.google.com/issues/new?component=460964) to get the documentation updated to point that out. – ianhanniballake Mar 03 '19 at 20:15
6

If you are not using the ktx dependency, e.g. androidx.test.ext:junit:1.1.2 you can do it like this:

@get:Rule
var activityScenarioRule = ActivityScenarioRule(MyActivity::class.java)
1

My example of usage ActivityScenarioRule<> for Java:

public class AdMobContainerImplTest {
    private static final String TAG = AdMobContainerImplTest.class.getSimpleName();
    @Rule
    public ActivityScenarioRule<ENDetailsActivity> mActivityRule = new ActivityScenarioRule<>(
            ENDetailsActivity.class);

    @Test
    public void testAdVisibility() {
        mActivityRule.getScenario().onActivity(activity -> {
            AdView ad = activity.findViewById(R.id.ad_banner);
            ad.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    Log.i(TAG, "ad loaded");
                    Assert.assertNotNull(ad);
                    onView(withId(R.id.ad_banner)).check(matches(isDisplayed()));
                }
            });
        });
    }
}
Yuliia Ashomok
  • 8,336
  • 2
  • 60
  • 69
0

I had a similar problem.
Changing dependency from testImplementation 'androidx.test.ext:junit:1.1.3'
to androidTestImplementation 'androidx.test.ext:junit:1.1.3' worked for me.

Nandkishor
  • 149
  • 2
  • 12
  • This only works for if you are running instrumentation tests is the androidTest folder. You need testImplementation for running unit tests in the test folder. – adrem7 Jan 12 '22 at 20:50