EcologicalViewHolder extends DataViewHolder<EcologicalSandwich> { @Override public void populate(EcologicalSandwich sandwich) { Context c = itemView.getContext(); name.setText(sandwich.getName()); String start = DateUtils.getFriendlyDate(c, sandwich.getStart()); String end = DateUtils.getFriendlyDate(c, sandwich.getEnd()); if (sandwich.isVegan()) { Drawable image = AppCompatResources.getDrawable(c, R.drawable.resto_vegan); name.setCompoundDrawablesWithIntrinsicBounds(null, null, image, null); } else { name.setCompoundDrawables(null, null, null, null); } dates.setText(String.format(c.getString(R.string.date_between), start, end)); String ingredientsString = TextUtils.join(", ", sandwich.getIngredients()); ingredients.setText(String.format(c.getString(R.string.resto_sandwich_ingredients), ingredientsString)); expandableLayout.setExpanded(adapter.isChecked(getAdapterPosition()), false); itemView.setOnClickListener(v -> { adapter.setChecked(getAdapterPosition()); expandableLayout.toggle(); }); } EcologicalViewHolder(View itemView, MultiSelectAdapter<EcologicalSandwich> adapter); @Override void populate(EcologicalSandwich sandwich);  }
@Test public void populate() { View view = inflate(R.layout.item_sandwich_eco); EcologicalSandwich sandwich = generate(EcologicalSandwich.class); @SuppressWarnings("unchecked") MultiSelectAdapter<EcologicalSandwich> adapter = mock(MultiSelectAdapter.class); when(adapter.isChecked(anyInt())).thenReturn(expanded); EcologicalViewHolder viewHolder = new EcologicalViewHolder(view, adapter); viewHolder.populate(sandwich); assertTextIs(sandwich.getName(), view.findViewById(R.id.name)); assertNotEmpty(view.findViewById(R.id.dates)); assertNotEmpty(view.findViewById(R.id.sandwich_ingredients)); ExpandableLayout layout = view.findViewById(R.id.expandable_layout); assertEquals(expanded, layout.isExpanded()); ArgumentCaptor<Integer> positionCaptor = ArgumentCaptor.forClass(Integer.class); view.performClick(); verify(adapter).setChecked(positionCaptor.capture()); assertEquals(RecyclerView.NO_POSITION, (int) positionCaptor.getValue()); assertEquals(!expanded, layout.isExpanded()); }