BaseFragmentFactory implements FragmentFactory { @Override @Nullable public Fragment createFragment(final int fragmentId) { return isFragmentProvided(fragmentId) ? onCreateFragment(fragmentId) : null; } BaseFragmentFactory(); @Nullable static String createFragmentTag(@NonNull final Class<?> classOfFactory, @NonNull final String fragmentName); @Override boolean isFragmentProvided(final int fragmentId); @Override @Nullable Fragment createFragment(final int fragmentId); @Override @Nullable String createFragmentTag(final int fragmentId);  }
@Test(expected = IllegalArgumentException.class) public void testCreateFragmentForNotInstantiableFragment() { final TestFactory factory = new TestFactory(); factory.createFragment(TestFactory.FRAGMENT_2); }
@Test public void testCreateFragment() { final BaseFragmentFactory factory = new TestFactory(); assertThat(factory.createFragment(TestFactory.FRAGMENT_NOT_PROVIDED), is(nullValue())); assertThat(factory.createFragment(TestFactory.FRAGMENT_1), instanceOf(TestFragment1.class)); assertThat(factory.createFragment(TestFactory.FRAGMENT_3), instanceOf(TestFragment3.class)); assertThat(factory.createFragment(TestFactory.FRAGMENT_4), instanceOf(TestFragment4.class)); }