### Question:
BundleContextServiceListener implements ServiceListener { @Override public void serviceChanged( ServiceEvent serviceEvent ) { if ( referenceToInstanceMap.containsKey( serviceEvent.getServiceReference() ) ) { Object instance = referenceToInstanceMap.get( serviceEvent.getServiceReference() ); LifecycleEvent type = LifecycleEvent.MODIFY; switch( serviceEvent.getType() ) { case ServiceEvent.MODIFIED: type = LifecycleEvent.MODIFY; break; case ServiceEvent.UNREGISTERING: type = LifecycleEvent.STOP; break; } delayedInstanceNotifierFactory.create( instance, type ).run(); } } BundleContextServiceListener( Map<ServiceReference, Object> referenceToInstanceMap,
                                       DelayedInstanceNotifierFactory delayedInstanceNotifierFactory ); @Override void serviceChanged( ServiceEvent serviceEvent );  }### Answer:
@Test public void testReferenceToInstanceMapDoesntContainKey() { bundleContextServiceListener.serviceChanged( new ServiceEvent( ServiceEvent.MODIFIED, serviceReference ) ); verifyNoMoreInteractions( delayedInstanceNotifierFactory ); }
@Test public void testModified() { Object instance = mock( Object.class ); referenceToInstanceMap.put( serviceReference, instance ); DelayedInstanceNotifier delayedInstanceNotifier = mock( DelayedInstanceNotifier.class ); when( delayedInstanceNotifierFactory.create( instance, LifecycleEvent.MODIFY ) ) .thenReturn( delayedInstanceNotifier ); bundleContextServiceListener.serviceChanged( new ServiceEvent( ServiceEvent.MODIFIED, serviceReference ) ); verify( delayedInstanceNotifier ).run(); }
@Test public void testUnregistering() { Object instance = mock( Object.class ); referenceToInstanceMap.put( serviceReference, instance ); DelayedInstanceNotifier delayedInstanceNotifier = mock( DelayedInstanceNotifier.class ); when( delayedInstanceNotifierFactory.create( instance, LifecycleEvent.STOP ) ) .thenReturn( delayedInstanceNotifier ); bundleContextServiceListener.serviceChanged( new ServiceEvent( ServiceEvent.UNREGISTERING, serviceReference ) ); verify( delayedInstanceNotifier ).run(); }
@Test public void testOther() { Object instance = mock( Object.class ); referenceToInstanceMap.put( serviceReference, instance ); DelayedInstanceNotifier delayedInstanceNotifier = mock( DelayedInstanceNotifier.class ); when( delayedInstanceNotifierFactory.create( instance, LifecycleEvent.MODIFY ) ) .thenReturn( delayedInstanceNotifier ); bundleContextServiceListener.serviceChanged( new ServiceEvent( ServiceEvent.REGISTERED, serviceReference ) ); verify( delayedInstanceNotifier ).run(); }