PerRequestObjectFactory implements ObjectFactory<T> { @Override public Object getInstance(ApplicationContext context) { ConstructorDescriptor constructorDescriptor = model.getConstructorDescriptors().get(0); Object object = constructorDescriptor.createInstance(context); List<FieldInjector> fieldInjectors = model.getFieldInjectors(); if (fieldInjectors != null && fieldInjectors.size() > 0) { for (FieldInjector injector : fieldInjectors) { injector.inject(object, context); } } doPostConstruct(object, context); return object; } PerRequestObjectFactory(T model); @Override Object getInstance(ApplicationContext context); @Override T getObjectModel();  }
@Test public void createsInstance() throws Exception { Object instance = new Object(); when(constructorDescriptor.createInstance(applicationContext)).thenReturn(instance); Object result = perRequestObjectFactory.getInstance(applicationContext); assertSame(instance, result); }
@Test public void injectsFields() throws Exception { Object instance = new Object(); when(constructorDescriptor.createInstance(applicationContext)).thenReturn(instance); perRequestObjectFactory.getInstance(applicationContext); verify(fieldInjector).inject(instance, applicationContext); }
@Test public void addsLifeCycleComponentInApplicationContext() throws Exception { Object instance = new Object(); when(constructorDescriptor.createInstance(applicationContext)).thenReturn(instance); perRequestObjectFactory.getInstance(applicationContext); assertEquals(1, lifecycleComponents.size()); assertSame(instance, lifecycleComponents.get(0).getComponent()); }