expect(factoryParameters.getFirstParameter()).andReturn(factoryParametersList.get(0)).anyTimes();
expect(factoryParameters.getParameters()).andReturn(factoryParametersList).anyTimes();
expect(factoryParameters.getServiceInterface()).andReturn(SomeService.class).anyTimes();
expect(factoryParameters.getServiceId()).andReturn("someService");
replay(factoryParameters);
ServiceImplementationFactory rootService = createMock(ServiceImplementationFactory.class);
final SomeService realCreatedService = createMock(SomeService.class);
expect(rootService.createCoreServiceImplementation(isA(ServiceImplementationFactoryParameters.class))).
andAnswer(new IAnswer<Object>() {
public Object answer() throws Throwable {
ServiceImplementationFactoryParameters object = (ServiceImplementationFactoryParameters) getCurrentArguments()[0];
// make sure being passed the proxy
assertTrue(Proxy.isProxyClass(object.getInvokingModule().getClass()), "Did not get passed a module proxy");
assertTrue(object.getInvokingModule().containsService(dependentServiceClass),
dependentServiceClass+": module does not have service with this interface");
// TODO List dependentService = (List) object.getInvokingModule().getService(dependentServiceClass);
return realCreatedService;
}
});
replay(rootService, realCreatedService);
InterceptorStack stack = new InterceptorStackImpl(log, servicePoint, rootService);
// create the interceptor
factory.createInterceptor(stack, invokingModule, parameters);
ServiceImplementationFactory intercepted = (ServiceImplementationFactory) stack.peek();
assertNotNull(intercepted);
SomeService result = (SomeService) intercepted.createCoreServiceImplementation(factoryParameters);
MockSwitcher switcher = (MockSwitcher) Proxy.getInvocationHandler(result);
assertSame(switcher.getRealService(), realCreatedService);
assertSame(switcher.getUnderlyingService(), realCreatedService);
// now tell factory that we always want the mock.