### Question:
Activator implements BundleActivator { public void start( BundleContext bundleContext ) { this.pentahoWebPackageBundleListener = createPentahoWebPackageService(); bundleContext.addBundleListener( this.pentahoWebPackageBundleListener ); addActiveBundles( bundleContext.getBundles() ); }  void start( BundleContext bundleContext ); void stop( BundleContext bundleContext );  }### Answer:
@Test public void bundleListenerIsAddedToBundleContextOnActivatorStart() { PentahoWebPackageBundleListener bundleListener = mock( PentahoWebPackageBundleListener.class ); Activator activator = createActivatorSpy( bundleListener ); BundleContext bundleContext = mock( BundleContext.class ); activator.start( bundleContext ); verify( bundleContext ).addBundleListener( bundleListener ); }
@Test public void listenerRegisterIsCalledForEveryActiveBundleOnActivatorStart() { PentahoWebPackageBundleListener bundleListener = mock( PentahoWebPackageBundleListener.class ); Activator activator = createActivatorSpy( bundleListener ); Collection<Bundle> activeBundles = new ArrayList<>(); int numberOfActiveBundles = 3; for ( int iBundle = 0; iBundle < numberOfActiveBundles; iBundle++ ) { activeBundles.add( createMockBundle( Bundle.ACTIVE ) ); } BundleContext bundleContext = mock( BundleContext.class ); doReturn( activeBundles.toArray( new Bundle[0] ) ).when( bundleContext ).getBundles(); activator.start( bundleContext ); verify( bundleListener, times( numberOfActiveBundles ) ).registerWebPackageServices( any() ); }
@Test public void listenerRegisterIsNotCalledForBundlesNotActiveOnActivatorStart() { PentahoWebPackageBundleListener bundleListener = mock( PentahoWebPackageBundleListener.class ); Activator activator = createActivatorSpy( bundleListener ); Collection<Bundle> nonActiveBundles = new ArrayList<>(); nonActiveBundles.add( createMockBundle( Bundle.UNINSTALLED ) ); nonActiveBundles.add( createMockBundle( Bundle.INSTALLED ) ); nonActiveBundles.add( createMockBundle( Bundle.RESOLVED ) ); nonActiveBundles.add( createMockBundle( Bundle.STARTING ) ); nonActiveBundles.add( createMockBundle( Bundle.STOPPING ) ); BundleContext bundleContext = mock( BundleContext.class ); doReturn( nonActiveBundles.toArray( new Bundle[0] ) ).when( bundleContext ).getBundles(); activator.start( bundleContext ); verify( bundleListener, never() ).registerWebPackageServices( any() ); }