10

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)
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

3 Answers3

14

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/)

flauschtrud
  • 690
  • 7
  • 23
  • 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 this – Adam Burley Mar 20 '21 at 21:26
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.

Community
  • 1
  • 1
amram99
  • 699
  • 8
  • 19
  • What does `setupActivity` do in AndroidX Test? – IgorGanapolsky Jun 17 '19 at 22:05
  • 2
    @IgorGanapolsky `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 http://robolectric.org/androidx_test/ – amram99 Jun 21 '19 at 13:38
  • 1
    More info can be found here https://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. – Victor Oliveira Oct 23 '19 at 20:48
2

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

Oscar Ivan
  • 819
  • 1
  • 11
  • 21