### Question:
DelayedInstanceNotifier implements Runnable { @Override public void run() { BeanFactory factory = null; try { factory = osgiPluginTracker.findOrCreateBeanFactoryFor( serviceObject ); } catch ( OSGIPluginTrackerException e ) { logger.error( "Error getting bean factory", e ); } if ( factory == null ) { ScheduledFuture<?> timeHandle = scheduler.schedule( this, 2, TimeUnit.SECONDS ); } else { List<ServiceReferenceListener> listeners = instanceListeners.get( serviceObject ); if ( listeners != null ) { for ( ServiceReferenceListener listener : listeners ) { listener.serviceEvent( eventType, serviceObject ); } } } } DelayedInstanceNotifier( OSGIPluginTracker osgiPluginTracker, LifecycleEvent eventType,
                                  Object serviceObject, Map<Object, List<ServiceReferenceListener>> instanceListeners,
                                  ScheduledExecutorService scheduler ); @Override void run();  }### Answer:
@Test public void testFactoryEqualNotNull() throws Exception { DelayedInstanceNotifier delayedInstanceNotifier = new DelayedInstanceNotifier( osgiPluginTracker, eventType, serviceObject, instanceListeners, scheduler ); ServiceReferenceListener listener = mock( ServiceReferenceListener.class ); List<ServiceReferenceListener> listeners = new ArrayList<ServiceReferenceListener>( Arrays.asList( listener ) ); instanceListeners.put( serviceObject, listeners ); BeanFactory beanFactory = mock( BeanFactory.class ); when( osgiPluginTracker.findOrCreateBeanFactoryFor( serviceObject ) ).thenReturn( beanFactory ); delayedInstanceNotifier.run(); verify( listener ).serviceEvent( eventType, serviceObject ); }
@Test public void testFactoryEqualNull() throws Exception { DelayedInstanceNotifier delayedInstanceNotifier = new DelayedInstanceNotifier( osgiPluginTracker, eventType, serviceObject, instanceListeners, scheduler ); when( osgiPluginTracker.findOrCreateBeanFactoryFor( serviceObject ) ).thenReturn( null ); delayedInstanceNotifier.run(); verify( scheduler ).schedule( delayedInstanceNotifier, 2, TimeUnit.SECONDS ); }
@Test public void testExceptionFromTracker() throws Exception { DelayedInstanceNotifier delayedInstanceNotifier = new DelayedInstanceNotifier( osgiPluginTracker, eventType, serviceObject, instanceListeners, scheduler ); when( osgiPluginTracker.findOrCreateBeanFactoryFor( serviceObject ) ) .thenThrow( new OSGIPluginTrackerException( "Error" ) ); delayedInstanceNotifier.run(); verify( scheduler ).schedule( delayedInstanceNotifier, 2, TimeUnit.SECONDS ); }