I have a simple PageAdapter implementation that works in the app, but I am unable to create a unit test for the Fragment containing it. The getCount() method in the adapter is called and my mock data returns a count of 1 item to display, but thereafter, instantiateItem never gets called, so I can't inflate the layout and populate the views. Following are the two adapter methods.
@Override
public int getCount() {
return v.getB().size(); // returns 1 with my test data
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View layout = layoutInflater.inflate(R.layout.page, container, false);
ButterKnife.inject(this, layout);
B b = v.getB().get(position);
configureLayoutForB(layout, b);
container.addView(layout, position);
return redemptionlayout;
}
And this is my unit test code
@Before
public void setup() {
bArrayList = new ArrayList<B>(2);
b0 = mock(B.class);
b1 = mock(B.class);
}
@Test
public void shouldShowPlainCode() {
final String testCode = "test code";
when(b0.getCode()).thenReturn(testCode);
bArrayList.add(b0);
final V v = mock(V.class);
when(v.getB()).thenReturn(bArrayList);
fragment = SVFragment.newInstance(voucher);
startFragment(fragment, TestSVActivity.class);
TextView codeView = (TextView) findViewById(R.id.code);
assertThat(codeView).containsText(testCode);
}
The whole test suite is run using Robolectric. Any insight or suggestions would be very welcome. Thanks.