I fill the Cursor
and FragmentManager
value inside the PageAdapter
class but during Test process it brings Cursor
and FragmentManager
as null for always.
How i can configure Test to get Cursor
and FragmentManager
with a valid value?
public class PagerAdapter extends FragmentStatePagerAdapter {
private Cursor mCursor;
public PagerAdapter(FragmentManager fm, Cursor aCursor) {
super(fm);
mCursor = aCursor;
}
Test code block is below:
@RunWith(MockitoJUnitRunner.class)
public class PagerAdapterTest extends Assert{
@Mock
private PagerAdapter mPagerAdapter;
private FragmentManager fm;
private Cursor mCursor;
@Before
public void setUp() throws Exception {
mPagerAdapter = new PagerAdapter(fm, mCursor);
}
Your problem is "simple" - in your test code:
private FragmentManager fm;
private Cursor mCursor;
are both null. SO this:
mPagerAdapter = new PagerAdapter(fm, mCursor);
Maybe you assumed that @Mock mocks all fields - nope, sorry: it only mocks the first field declared afterwards!
So your code is just doing new PageAdapter(null, null)
. The point is: you need to mock various core infrastructure elements that are provided by the Android system in the "real world".
And beyond that, you seem to not understand how mocking and unit tests come together. You want to test your PageAdapter class - and in that case it makes no sense to mock the PageAdapter class. You create mocks for those objects that you need to feed into your class under test to test that class. In that sense, you should first study the basics of unit testing and mocking (ideally without the complexity added by Android). Start reading here for example.
And then, when you "got that part", read and follow the extensive documentation that explains to you how to establish a "working" unit test environment for Android. See here for example.