GridMultiWidget extends BaseGridWidget { @Override public IAnswerData getAnswer() { List<Selection> answers = new ArrayList<>(); for (int selectedItem : selectedItems) { SelectChoice sc = items.get(selectedItem); answers.add(new Selection(sc)); } return answers.isEmpty() ? null : new SelectMultiData(answers); } GridMultiWidget(Context context, QuestionDetails questionDetails); @Override IAnswerData getAnswer(); @Override void setChoiceSelected(int choiceIndex, boolean isSelected);  }
@Test public void getAnswerShouldReflectWhichSelectionsWereMade() { GridMultiWidget widget = getWidget(); assertNull(widget.getAnswer()); List<SelectChoice> selectChoices = getSelectChoices(); List<String> selectedValues = new ArrayList<>(); boolean atLeastOneSelected = false; for (int i = 0; i < widget.selectedItems.size(); i++) { boolean shouldBeSelected = random.nextBoolean(); if (shouldBeSelected) { widget.selectedItems.add(i); } atLeastOneSelected = atLeastOneSelected || shouldBeSelected; if (shouldBeSelected) { SelectChoice selectChoice = selectChoices.get(i); selectedValues.add(selectChoice.getValue()); } } if (!atLeastOneSelected) { int randomIndex = Math.abs(random.nextInt()) % widget.items.size(); widget.selectedItems.add(randomIndex); SelectChoice selectChoice = selectChoices.get(randomIndex); selectedValues.add(selectChoice.getValue()); } SelectMultiData answer = (SelectMultiData) widget.getAnswer(); @SuppressWarnings("unchecked") List<Selection> answerSelections = (List<Selection>) answer.getValue(); List<String> answerValues = selectionsToValues(answerSelections); for (String selectedValue : selectedValues) { assertTrue(answerValues.contains(selectedValue)); } }