views:

62

answers:

1

I'm currently making use of Castle Windsor version 2.1 as my container and would like to perform integration tests using the services registered with it.

Currently, I do this my using the Common Service Locator to retrieve my service instance and perform my integration tests against it as such:

var myService = ServiceLocator.Current.GetInstance<IMyService>();
// do stuff with myService

What I'd ideally like to do is have my service dependencies injected into my NUnit test fixture automatically. Spring seems to offer this functionality, but I can't locate anything similar using Castle.

Can anyone point me in the right direction?

Edit:

I'm sure everyone has (valid) points on whether or not this is a good idea; let's just assume that it is in this scenario...Can anyone tell me how this could be accomplished using Windsor?

A: 

If you're coming to Windsor from Spring/Spring.net you'll find that there are a number of things these projects don't agree on. This is one of them. As a Windsor advocate, I'd never use something like Spring's AbstractDependencyInjectionSpringContextTests. Injecting things into a test just seems wrong, but then again, like I said, I'm biased.

If you want to do an integration test, just create a new container instance, add whatever components you need, and run whatever you want to test, e.g:

[Test]
public void TestComponentThatDependsOnA() {
  var container = new WindsorContainer();
  container.Register(Component.For<MyComponentA>());
  container.Register(Component.For<ComponentThatDependsOnA>());
  var a = container.Resolve<ComponentThatDependsOnA>();
  var result = a.DoSomething();
  Assert.AreEqual("ok", result);
}

If you have registrations neatly wrapped in installers (as you should) you can reuse them in your tests, making tests more concise.

If you have XML config, you can easily load it with Configuration.FromXmlFile().

Also, there is no need to use Common Service Locator here.

Also useful for some integration tests is an auto-mocking container, which will automatically mock out services (unless you override them with concrete ones, of course).

Mauricio Scheffer
Thanks for the comments; I am using an installer to wire all my dependancies when the integration test fixture is loaded as you've suggested. I actually perform all of this in a seperate assembly, so the integration test really has no idea that windsor is managing all of this (thus the reason for using the common service locator to retrieve my dependancies). I suppose it could be argued that I'm being lazy with this approach, but my dependency graph is pretty complicated, so this is the only feasible way I could think of to eliminate a ton of setup work for my integration test fixture.
DanP
For reference, I'm using something along these lines to wire up my dependencies: http://fabiomaulo.blogspot.com/2009/11/guywire.html
DanP