private void MockApplicationContextForType(Type type)
        {
            if (!TypesToSubrogateSet.Contains(type))
            {
                return;
            }

            IObjectDefinition objectDefinition;

            if (!TypesToSpySubrogateSet.Contains(type))
            {
                objectDefinition = ObjectDefinitionBuilder.GenericObjectDefinition(
                    typeof(SurrogateFactoryObject))
                                   .AddPropertyValue("SurrogateType", SurrogateType.Mock)
                                   .AddPropertyValue("TargetObjectType", type)
                                   .ObjectDefinition;
            }
            else
            {
                var objectToSpy = GetTargetObject(GetObjectFromType(type));
                objectDefinition = ObjectDefinitionBuilder.GenericObjectDefinition(
                    typeof(SurrogateFactoryObject))
                                   .AddPropertyValue("SurrogateType", SurrogateType.Spy)
                                   .AddPropertyValue("TargetObjectType", type)
                                   .AddPropertyValue("ObjectToFake", objectToSpy)
                                   .ObjectDefinition;
            }

            var objectNameList = GetAppContextObjectNameListFromType(type);

            var nameList             = objectNameList as string[] ?? objectNameList.ToArray();
            var objectDefinitionName = nameList.Count() == 1 ? nameList.First() : type.FullName;

            if (_applicationContext.ContainsObjectDefinition(objectDefinitionName))
            {
                // Save the original object definition so it can be restored afterwards.
                _originalObjectDefinitionsDictionary[objectDefinitionName] =
                    _applicationContext.GetObjectDefinition(objectDefinitionName);
            }
            else
            {
                //This version of Spring.NET does not support removing object definition from the ApplicationContext.
                //Therefore, do not support this for now.
                //Once supported, add the surrogate ObjectDefinition to be removed later.
                throw new NotSupportedException("Cannot register a surrogate ObjectDefinition since it cannot be " +
                                                "removed afterwards");
            }
            _applicationContext.ReplaceObjectDefinition(objectDefinitionName, objectDefinition);
        }
        protected override object GetObjectFromField(FieldInfo fieldInfo, object o)
        {
            var target = fieldInfo.GetValue(o);

            if (TypesToSpySubrogateSet.Contains(fieldInfo.FieldType))
            {
                var names = GetAppContextObjectNameListFromType(fieldInfo.FieldType);
                if (names.Any())
                {
                    // TODO: Deal with the case that there are several names. Using always the First
                    // may not be okay in all cases.
                    var spyObjectDef = _applicationContext.GetFactoryObject(names.First());
                    if (spyObjectDef is SurrogateFactoryObject)
                    {
                        target = (spyObjectDef as SurrogateFactoryObject).ObjectToFake;
                    }
                }
            }
            while (AopUtils.IsAopProxy(target))
            {
                target = ((IAdvised)target).TargetSource.GetTarget();
            }
            return(target);
        }