Having trouble testing RadioGroup with RoboGuice + Robolectric
-
30-05-2021 - |
Question
I have looked at this a bunch of different ways and with what little hair I have left, I thought I would put it out there in the hope someone has already tried this.
I am trying to write a Robolectric test of my Roboguice enabled Activity. Specifically, I am trying to write test that ensure the behavior of a RadioGroup.
The problem is that when running the test, the RadioGroup does not act like a RadioGroup and enforce the only-one-RadioButton-checked-at-a-time behaviour. I can see by both Asserting and with the debugger that I can check all three of the buttons in the group at once.
The RadioGroup is very straightforward:
<RadioGroup
android:id="@+id/whenSelection"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/whenToday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/today" />
<RadioButton
android:id="@+id/whenYesterday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yesterday" />
<RadioButton
android:id="@+id/whenOther"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/earlier" />
</RadioGroup>
I should point on then what I run the app, the behaviour is what I expect (that if I click on any of the radio buttons only that one remains checked, and the other two are unchecked). So, in theory, this test should pass:
-- snip--
Assert.assertTrue(whenToday.isChecked());
Assert.assertFalse(whenYesterday.isChecked());
Assert.assertFalse(whenOther.isChecked());
whenYesterday.performClick();
Assert.assertTrue(whenYesterday.isChecked());
Assert.assertFalse(whenToday.isChecked());
-- snip --
But, what happens is that the last assertion fails, and the debugger confirms that the first button whenToday has remained checked.
Here is the full test class:
@RunWith(InjectedTestRunner.class)
public class MyTest {
@Inject ActivityLogEdit activity;
RadioButton whenToday;
RadioButton whenYesterday;
RadioButton whenOther;
@Before
public void setUp() {
activity.setIntent(new Intent());
activity.onCreate(null);
whenSelection = (RadioGroup) activity.findViewById(R.id.whenSelection);
whenToday = (RadioButton) activity.findViewById(R.id.whenToday);
whenYesterday = (RadioButton) activity.findViewById(R.id.whenYesterday);
whenOther = (RadioButton) activity.findViewById(R.id.whenOther);
}
@Test
public void checkDateSelectionInitialState() throws Exception {
Assert.assertTrue(whenToday.isChecked());
Assert.assertFalse(whenYesterday.isChecked());
Assert.assertFalse(whenOther.isChecked());
Assert.assertEquals(View.GONE, logDatePicker.getVisibility());
whenYesterday.performClick();
Assert.assertTrue(whenYesterday.isChecked());
Assert.assertFalse(whenToday.isChecked());
}
}
I have tried this every different way I can think of. I have the feeling I am doing something stupid or missing some basic concept. Please help!
Andrew
Solution
I received an answer to a post on the Robolectric Group from one of the guys who develops it:
The functionality you're talking about (checking a radio button will uncheck all other radio buttons in the group) is not yet implemented in Robolectric. Feel free to file a feature request: https://github.com/pivotal/robolectric/issues/ If I remember correctly, try changing your tests to use getCheckedRadioButtonId() on the RadioGroup instead of isChecked() on the RadioButtons--I believe that is implemented.
Note that I also tried getCheckedRadioButtonId()
and it is also unimplemented (always returns -1).
Andrew