FragmentItem { @SuppressWarnings("TryWithIdenticalCatches") @Nullable public Fragment newFragmentInstance(@Nullable final Bundle arguments) { if (type.equals(Fragment.class)) { return null; } try { final Fragment fragment = type.newInstance(); if (arguments != null) { fragment.setArguments(arguments); } return fragment; } catch (InstantiationException e) { Log.e( TAG, "Failed to instantiate a new fragment instance class of(" + type + "). " + "Make sure that this fragment class is accessible and has public empty constructor.", e ); } catch (IllegalAccessException e) { Log.e( TAG, "Failed to instantiate a new fragment instance class of(" + type + "). " + "Make sure that this fragment class is accessible and has public empty constructor.", e ); } return null; } FragmentItem(final int id, @NonNull final Class<? extends Fragment> type);  FragmentItem(final int id, @NonNull final Class<? extends Fragment> type, @Nullable final String tag); @SuppressWarnings("TryWithIdenticalCatches") @Nullable Fragment newFragmentInstance(@Nullable final Bundle arguments); final int id; final Class<? extends Fragment> type; final String tag; }
@SuppressWarnings("ConstantConditions") @Test public void testNewFragmentInstanceWithArguments() { final Bundle args = new Bundle(); final FragmentItem item = new FragmentItem(1, TestFragment.class); final Fragment fragment = item.newFragmentInstance(args); assertThat(fragment, is(notNullValue())); assertThat(fragment.getArguments(), is(args)); }
@SuppressWarnings("ConstantConditions") @Test public void testNewFragmentInstanceWithoutArguments() { final FragmentItem item = new FragmentItem(1, TestFragment.class); final Fragment fragment = item.newFragmentInstance(null); assertThat(fragment, is(notNullValue())); assertThat(fragment.getArguments(), is(nullValue())); }
@Test public void testNewFragmentInstanceForFragmentClass() { final FragmentItem item = new FragmentItem(1, Fragment.class); assertThat(item.newFragmentInstance(new Bundle()), is(nullValue())); }
@Test public void testNewFragmentInstanceForAbstractFragment() { final FragmentItem item = new FragmentItem(1, TestAbstractFragment.class); assertThat(item.newFragmentInstance(new Bundle()), is(nullValue())); }
@Test public void testNewFragmentInstanceForFragmentWithPrivateConstructor() { final FragmentItem item = new FragmentItem(1, TestFragmentWithPrivateConstructor.class); assertThat(item.newFragmentInstance(new Bundle()), is(nullValue())); }