ActionBarFragment extends BaseFragment { @Nullable protected androidx.appcompat.app.ActionBar getSupportActionBar() { if (activityDelegate == null) { throw new IllegalStateException( "Cannot access support ActionBar. " + getClass() + " is not attached " + "to the parent activity yet or it has been already detached!" ); } return activityDelegate.getSupportActionBar(); }  @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 testGetSupportActionBar() { final ActionBarFragment fragment = new TestFragment(); final ActivityDelegate mockDelegate = mock(ActivityDelegate.class); fragment.activityDelegate = mockDelegate; final androidx.appcompat.app.ActionBar mockActionBar = mock(androidx.appcompat.app.ActionBar.class); when(mockDelegate.getSupportActionBar()).thenReturn(mockActionBar); assertThat(fragment.getSupportActionBar(), is(mockActionBar)); verify(mockDelegate).getSupportActionBar(); verifyNoMoreInteractions(mockDelegate); }
@Test(expected = IllegalStateException.class) public void testGetSupportActionBarWhenNotAvailable() { final TestFragment fragment = new TestFragment(); fragment.getSupportActionBar(); }