PonyServiceRegistry { public static void registerPonyService(final PonyService service) { final Set<Class<?>> classes = new HashSet<>(); getGeneralizations(service.getClass(), classes); classes.stream().filter(PonyService.class::isAssignableFrom).forEach(clazz -> { if (!registeredServices.containsKey(clazz)) { registeredServices.put(clazz, service); log.info("Service registered #{}", clazz); } else { throw new IllegalArgumentException("Already registered service #" + clazz); } }); }  static void registerPonyService(final PonyService service); static T getPonyService(final Class<T> clazz);  }
@Test public void testRegisterPonyService() { final PonyService service1 = Mockito.mock(ServiceA.class); PonyServiceRegistry.registerPonyService(service1); final PonyService service2 = Mockito.mock(ServiceB.class); PonyServiceRegistry.registerPonyService(service2); assertEquals(service1, PonyServiceRegistry.getPonyService(ServiceA.class)); assertEquals(service2, PonyServiceRegistry.getPonyService(ServiceB.class)); }
@Test(expected = IllegalArgumentException.class) public void testRegistedPonyServiceTwice() { final PonyService service1 = Mockito.mock(ServiceA.class); PonyServiceRegistry.registerPonyService(service1); PonyServiceRegistry.registerPonyService(service1); }