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)
You can access the ActivityScenario instance via getScenario() . You may finish your activity manually in your test, it will not cause any problems and this rule does nothing after the test in such cases. This rule is an upgraded version of the now deprecated ActivityTestRule .
setupActivity() is deprecated in Android unit test. Save this question.
Robolectric is a framework that allows you to write unit tests and run them on a desktop JVM while still using Android API. Robolectric provides a JVM compliant version of the android. jar file.
ActivityScenario provides APIs to start and drive an Activity's lifecycle state for testing. It works with arbitrary activities and works consistently across different versions of the Android framework. The ActivityScenario API uses Lifecycle.
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/)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With