Android Roboelectric 3.0 testing next activity –ch

2019-08-07 12:01发布

I have scenario like this:--

I have three activities ActivityA(launcher activity), ActivityB, ActivityC Now in Activity A I read the Application context to decide whether to start ActivityB or ActivityC.

But even after setting the context value manual, the ActivityA is not updated and second test case fails. Any helps?

    private ActivityA activityA;
    private ShadowActivity shadowActivity;
    @Before
    public void setUp() {
        activityA = Robolectric.setupActivity(ActivityA.class);
        assertNotNull("ActivityA not intsantiated", activityA);
        shadowActivity = Shadows.shadowOf(activityA);
    }

    @Test
    public void shouldStartActivityB() throws Exception
    {

        Intent intent = shadowActivity.peekNextStartedActivity();;
        assertEquals(ActivityB.class.getCanonicalName(), intent.getComponent().getClassName());
    }

      @Test
    public void shouldStartMainActivity() throws Exception
    {
ApplicationSettings.setWizardState(RuntimeEnvironment.application,  22);
        ShadowLog.d("Wizard state value", "" +ApplicationSettings.getWizardState(RuntimeEnvironment.application));// it prints 22
        activityA= Robolectric.setupActivity(ActivityA.class);
        shadowActivity = Shadows.shadowOf(activityA);
        Intent intent = shadowActivity.peekNextStartedActivity();
        ShadowLog.d("target activity is", intent.getComponent().getClassName());// This prints ActivityB instead of ActivityC
        assertEquals(ActivityC.class.getCanonicalName(), intent.getComponent().getClassName()); // this test case fails
    }

0条回答
登录 后发表回答