mardi 25 août 2015

Multi-threaded unit testing with Spring.net IOC

I'm using Spring.net (not by choice) to provide IoC wireup for dependency injection. In unit tests, I'm using code to create a spring GenericApplicationContext for dependency resolution as such:

var appContext = new GenericApplicationContext() as IConfigurableApplicationContext;
appContext.ObjectFactory.RegisterSingleton(nameof(IMemberConverter), MemberConverter);
appContext.ObjectFactory.RegisterSingleton(nameof(IServiceClientProvider), ServiceClientProvider);
ContextRegistry.RegisterContext(appContext);

The problem is that ContextRegistry only allows for one context in it's static self (as far as I can tell) and when I have multiple unit tests trying to set up the context in parallel, I start to run into issues with the context already being present or set up differently than a specific test is expecting (all sorts of the usual multi-threaded with static dependency race conditions you would expect). Is there a way to use code based config for spring.net IOC that allows a multi-threaded unit test environment to work? I'd really like to keep everything in code if possible.

I did see that when you check if the context is registered with ContextRegistry.IsContextRegistered(string contextName), you have to provide the name of the context you want to check for, so perhaps there's a way to register a context specific to a test in the ContextRegistry. That would be lovely.

Aucun commentaire:

Enregistrer un commentaire