### Question:
Activator implements BundleActivator, ServiceListener { @Override public void stop( BundleContext bundleContext ) throws Exception { AccessManagerFactoryTracker managerFactoryTracker = getAccessManagerFactoryTracker(); if (null != managerFactoryTracker) { managerFactoryTracker.close(); } }  @Override void serviceChanged( ServiceEvent event ); @Override void start( BundleContext bundleContext ); @Override void stop( BundleContext bundleContext );  }### Answer:
@Test public void shouldCloseAccessManagerFactoryTrackerOnStopOnlyWhenOpened() throws Exception { BundleContext bundleContext = mock(BundleContext.class); final AccessManagerFactoryTracker accessManagerFactoryTracker = mock(AccessManagerFactoryTracker.class); Activator activator = new Activator() { @Override protected AccessManagerFactoryTracker getAccessManagerFactoryTracker() { return accessManagerFactoryTracker; } }; activator.stop(bundleContext); verify(accessManagerFactoryTracker).close(); }
@Test public void shouldNotCloseAccessManagerFactoryTrackerOnStopBecauseNotOpened() throws Exception { BundleContext bundleContext = mock(BundleContext.class); final AccessManagerFactoryTracker accessManagerFactoryTracker = mock(AccessManagerFactoryTracker.class); Activator activator = new Activator() { @Override protected AccessManagerFactoryTracker getAccessManagerFactoryTracker() { return null; } }; activator.stop(bundleContext); verify(accessManagerFactoryTracker, never()).close(); }