ActionBarFragment extends BaseFragment { @Nullable protected ActionBar getActionBar() { if (activityDelegate == null) { throw new IllegalStateException( "Cannot access ActionBar. " + getClass() + " is not attached " + "to the parent activity yet or it has been already detached!" ); } return activityDelegate.getActionBar(); }  @Override void onCreate(@Nullable final Bundle savedInstanceState); @Override void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater); @Override void onActivityCreated(@Nullable final Bundle savedInstanceState); void invalidateActionBar();  }
@Test public void testGetActionBar() { final ActionBarFragment fragment = new TestFragment(); final ActivityDelegate mockDelegate = mock(ActivityDelegate.class); fragment.activityDelegate = mockDelegate; final ActionBar mockActionBar = mock(ActionBar.class); when(mockDelegate.getActionBar()).thenReturn(mockActionBar); assertThat(fragment.getActionBar(), is(mockActionBar)); verify(mockDelegate).getActionBar(); verifyNoMoreInteractions(mockDelegate); }
@Test(expected = IllegalStateException.class) public void testGetActionBarWhenNotAvailable() { final TestFragment fragment = new TestFragment(); fragment.getActionBar(); }