Unable to resolve activity for Intent robolectric ActivityScenarioRule
Asked Answered
G

4

11

In my robolectric test i wrote a

  @Rule
  public ActivityScenarioRule<AppCompatActivity> activityScenarioRule =
      new ActivityScenarioRule<>(AppCompatActivity.class);

  @Rule
  public ActivityScenarioRule<FragmentUtilActivity> activityScenarioRule2 =
      new ActivityScenarioRule<>(FragmentUtilActivity.class);

and an inner class:

  private static class FragmentUtilActivity extends FragmentActivity {
    public static int anchorId = 200;
    private StandaloneAccountMenuDialogFragment<FakeAccount> dialogFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      LinearLayout view = new LinearLayout(this);
      view.setId(anchorId);
      setContentView(view);
    }
  }

but then when I run the code, it fails.

What's the reason the first rule works but the second not?

Unable to resolve activity for Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.myComp..internal/.StandaloneAccountMenuDialogFragmentTest$FragmentUtilActivity } -- see https://github.com/robolectric/robolectric/pull/4736 for details
java.lang.RuntimeException: 
    at org.robolectric.android.fakes.RoboMonitoringInstrumentation.startActivitySyncInternal(RoboMonitoringInstrumentation.java:48)
    at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:34)
    at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:205)
    at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:182)
    at androidx.test.ext.junit.rules.ActivityScenarioRule.lambda$new$0(ActivityScenarioRule.java:68)
    at androidx.test.ext.junit.rules.ActivityScenarioRule.before(ActivityScenarioRule.java:82)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:546)
    at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:252)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Gaffer answered 11/4, 2019 at 14:55 Comment(0)
H
16

I just ran into the same problem and realized that I forgot to include

android {
    ...
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

in my build.gradle of a new project.

The result was that the manifest could not be used by Robolectric.

(see http://robolectric.org/getting-started/)

Headreach answered 23/2, 2020 at 13:6 Comment(1)
Thank you so much! I was going crazy reading through the issue log linked from the Robolectric error, but it was just something as simple as thisMneme
S
5

Robolectric now requires activities to be defined in a manifest. From the the known issues section of the release notes:

Activities must now be explicitly declared in a manifest (as is the case for normal Android); however, Android Gradle Plugin doesn't currently merge a test manifest.

This is problematic for libraries because anything put in the manifest will be merged into the manifests of consumers of the libraries. There is a workaround but it requires creating a new Gradle module. Hopefully the AGP bug will be fixed soon. In the mean time, I hope to use the deprecated Robolectric.setupActivity() approach.

Strontium answered 22/5, 2019 at 21:32 Comment(3)
What does setupActivity do in AndroidX Test?Origin
@Origin setupActivity does the same thing it did before. However, I ended up not using it. It's better to use ActivityScenario if possible since it's the new supported API. See robolectric.org/androidx_testStrontium
More info can be found here github.com/robolectric/robolectric/pull/4736 - As you said, I hope they fix that soon because this is really what I could call "workaround". Doesn't make any sense.Familiarity
I
5

If you are trying to run Robolectric tests in order to test some composable make sure you include in the manifest of your module this <activity android:name="androidx.activity.ComponentActivity" />. If this solves the problem then the issue is not due Robolectric instead Jetpack Compose setup for tests "RuntimeException: Could not launch activity...Unable to resolve activity for Intent" when running Jetpack Compose UI tests with createComposeRule

Indefatigable answered 15/11, 2022 at 18:51 Comment(1)
I was trying to test compose multiplatform with Robolectric and getting this error, this suggestion fixed it for me.Guyot
C
0

If you are using build.gradle.kts you are probably missing:

android {
    ...
    testOptions {
        unitTests {
            isReturnDefaultValues = true
            isIncludeAndroidResources = true
        }
    }
}
Carbamidine answered 5/2 at 23:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.