2

I am new to Robolectric and I am trying to test the text from a custom button. My custom button is a RelativeLayout composed from a Button and a ProgressBar. When I run the application, the text from custom attributes is correctly set to the button. But when I use Robolectric, the text is null.

Searching I have found this: Robolectric TypedArray getString() returns null or empty string but there is little information about how should I use the ShadowTypedArray.

From Robolectric site they say: you would annotate the test in question with @Config(shadows={MyShadowBitmap.class})

but this does not work for me:

@Test
@Config(shadows={ShadowTypedArray.class})
public void testLoginButton_shouldDisplayLoginText() throws Exception {
    Activity activity = Robolectric.buildActivity(LoginActivity.class).create().get();

    CustomProgressButton loginButton = (CustomProgressButton ) activity.findViewById(R.id.sign_in_button);

    String loginText = loginButton != null ? Strings.toString(loginButton.getText()) : null;
    assertThat(loginText, equalTo(activity.getString(R.string.action_sign_in_short)));
    assertThat(loginText, equalTo("LOG IN"));
}

Should I create a ShadowCustomProgressButton or what I am doing wrong? Thanks.

4

0 回答 0