2

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.

drew
  • 2,371
  • 2
  • 19
  • 27
  • I think you might be missing a call to 'visible' on the ActivityController. But I remember I did have to do some fiddling to get mine working. Is the startFragment call I see there standard or have you got some other code not shown? – DDD Apr 07 '14 at 22:00
  • 1
    Hey guy, I have got stuck in similar situation. Have you solved this problem? – Qian Sijianhao Aug 03 '17 at 02:48

0 Answers0