ComponentInjector { public <T> T inject(T target) { Arrays.stream(target.getClass().getDeclaredFields()) .filter(this::isInjectTarget) .forEach(f -> injectField(target, f)); postConstruct(target); return target; } ComponentInjector(Map<String, SystemComponent> components); T inject(T target);  }
@Test public void wrongNamedInject() { componentMap.put("myNameIsAAA", new TestComponent("A")); componentMap.put("MyNameIsB", new TestComponent("B")); ComponentInjector injector = new ComponentInjector(componentMap); InjectTarget2 target2 = new InjectTarget2(); try { injector.inject(target2); fail("MisconfigurationException will occur"); } catch (MisconfigurationException ex) { assertThat(ex.getSolution().contains("MyNameIsB")).isTrue(); } }
@Test public void test() { componentMap.put("myNameIsA", new TestComponent("A")); ComponentInjector injector = new ComponentInjector(componentMap); InjectTarget1 target1 = new InjectTarget1(); injector.inject(target1); assertThat(target1.tc.getId()).isEqualTo("A"); }
@Test public void testNamedInject() { componentMap.put("myNameIsA", new TestComponent("A")); componentMap.put("myNameIsB", new TestComponent("B")); ComponentInjector injector = new ComponentInjector(componentMap); InjectTarget2 target2 = new InjectTarget2(); injector.inject(target2); assertThat(target2.tc.getId()).isEqualTo("B"); }