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); }