danikenan
09-29-2005, 06:02 AM
For unit testing it is important to encapsulate all the code in the test and better not to use external files if possible. I would like to create an XmlApplicationContext from a rather simple and short xml string embeded as local variable of a test method.
How can I do this?
TIA
Rick Evans
09-29-2005, 09:45 AM
Hi
Short answer : the API as it currently stands doesn't have a way to do what you want.
Long answer...
For unit testing it is important to encapsulate all the code in the test and better not to use external files if possible.
Indeed, 100% agree with you there. However, you have said 'unit testing'... unit testing tests one class in isolation, so why do you need an application context? You can just mock / stub out the dependencies required by the unit under test, and manually set them using plain old C# / VB.NET language ctor args / property setters. Integration testing is another matter, and for that I would use an application context, and I would use one or more external files containing my object definitions. I realise that this answer is veering close to TDD pedantry (gee, I sound like I'm giving you a lecture) :D, so I'll stop there.
This is mainly down to the reasons I have outlined above, and because we want to keep in synch with the Spring Java API as much as possible. If you only need the wiring together features of the application context, you can use the simpler XmlObjectFactory (http://www.springframework.net/doc/api/html/Spring.Objects.Factory.Xml.XmlObjectFactory.html). This can be configured from a string because it takes an IResource (http://www.springframework.net/doc/api/html/Spring.Core.IO.IResource.html) as a ctor arg; to wit...
const string xml = @"<objects></objects/>";
IObjectFactory factory = new XMLObjectObjectFactory (new InputStreamResource(new MemoryStream(Encoding.UTF8.GetBytes(xml))));
// use factory.GetObject("object") etc
Agony huh? Mmm, I've just noticed your JIRA issue (http://opensource2.atlassian.com/projects/spring/browse/SPRNET-199). Good post. We do have an issue (http://opensource2.atlassian.com/projects/spring/browse/SPRNET-116) for the ConfigurableResourceLoader support, but it has been languishing in limbo for some time. I'll go bump it up a priority.
If you do need the extra features of an application context (automatic singleton instantiation, message resource lookup, etc), then... well, then you are unfortunately out of luck. The ConfigurableResourceLoader support really does need to be addressed. I'll get cracking.
Ciao
Rick
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.