ImplFromPropertiesFactory { public T create() { return this.injector .getInstance(getImplementationClass()); } ImplFromPropertiesFactory(Injector injector,
                                     NinjaProperties ninjaProperties,
                                     String propertyName,
                                     Class<? extends T> targetClass,
                                     String defaultClassName,
                                     boolean deferResolveUntilGet,
                                     Logger logger); T create(); Class<? extends T> getImplementationClass();  }
@Test public void defaultImplementation() { NinjaPropertiesImpl ninjaProperties = new NinjaPropertiesImpl(NinjaMode.test); Injector injector = Guice.createInjector(new BaseAndClassicModules(ninjaProperties)); ImplFromPropertiesFactory<MockInterface> factory = new ImplFromPropertiesFactory<>( injector, ninjaProperties, "i.am.a.test.implementation", MockInterface.class, "ninja.utils.ImplFromPropertiesFactoryTest$MockInterfaceImpl", false, logger); MockInterface mockObject = factory.create(); assertThat(mockObject, instanceOf(MockInterfaceImpl.class)); }
@Test public void missingImplementationDeferredUntilGet() { NinjaPropertiesImpl ninjaProperties = new NinjaPropertiesImpl(NinjaMode.test); ninjaProperties.setProperty("i.am.a.test.implementation", "does_not_exist"); Injector injector = Guice.createInjector(new BaseAndClassicModules(ninjaProperties)); ImplFromPropertiesFactory<MockInterface> factory = new ImplFromPropertiesFactory<>( injector, ninjaProperties, "i.am.a.test.implementation", MockInterface.class, null, true, logger); thrown.expect(RuntimeException.class); factory.create(); }
@Test public void configuredImplementation() { NinjaPropertiesImpl ninjaProperties = new NinjaPropertiesImpl(NinjaMode.test); ninjaProperties.setProperty("i.am.a.test.implementation", "ninja.utils.ImplFromPropertiesFactoryTest$MockInterfaceImpl"); Injector injector = Guice.createInjector(new BaseAndClassicModules(ninjaProperties)); ImplFromPropertiesFactory<MockInterface> factory = new ImplFromPropertiesFactory<>( injector, ninjaProperties, "i.am.a.test.implementation", MockInterface.class, null, false, logger); MockInterface mockObject = factory.create(); assertThat(mockObject, instanceOf(MockInterfaceImpl.class)); }
@Test public void implementationNotAnInstanceOfTarget() { NinjaPropertiesImpl ninjaProperties = new NinjaPropertiesImpl(NinjaMode.test); ninjaProperties.setProperty("i.am.a.test.implementation", "java.lang.Object"); Injector injector = Guice.createInjector(new BaseAndClassicModules(ninjaProperties)); ImplFromPropertiesFactory<MockInterface> factory = new ImplFromPropertiesFactory<>( injector, ninjaProperties, "i.am.a.test.implementation", MockInterface.class, null, true, logger); thrown.expect(RuntimeException.class); MockInterface mockObject = factory.create(); }