0

I have a button in my activity. Once clicked, it checks user registration status. If registered then shows a dialog otherwise starts registration activity.

I set required flags that says user has registered. I perform click and expect the dialog must be created but after debug I see this is null :(

This is my code:

@Test
    public void testSignupButton()
    {
        PreferenceUtils.setSessionId(activity, "sessionId");
        assertTrue(PreferenceUtils.isActivated(activity));

        btnSignUp.performClick();

        Dialog dialog = ShadowDialog.getLatestDialog(); // << dialog is null
        ShadowDialog loginDialogFragment = Shadows.shadowOf(dialog); // Test fails here since dialog is null
        assertThat(loginDialogFragment.getTitle().toString(), equalTo("TestDialogFragment"));
    }

Any idea, would be appreciated, thanks.

Hesam
  • 52,260
  • 74
  • 224
  • 365

1 Answers1

0

omg, I found where was my problem. I had activity = Robolectric.buildActivity(HitchWelcomeActivity.class).create().get(); in my setupView method which is wrong. In order to get real activity I should have visible() method too.

So, I changed above code to following and my problem fixed.

    @Before
    public void setUp()
    {
        activity = Robolectric.buildActivity(WelcomeActivity.class)
                .create()
                .start()
                .resume()
                .visible()
                .get();

        btnSignUp = (Button) activity.findViewById(R.id.dialog_welcome_sign_up);
        btnSkip = (TextView) activity.findViewById(R.id.dialog_welcome_next_time);
    }
Hesam
  • 52,260
  • 74
  • 224
  • 365