Jan 17th, 2013, 02:25 PM
Best Practices: Activating IoC container for test classes in multi-module projects?
I've got a multi module project For simplicity, let's discuss two modules, "Base", which contains all the domain objects, all their JPA persistence-related ITDs, and the declarative stuff that makes Spring work (application-context.xml, persistence.xml, etc.), and "Other", which includes Base as a dependency.
Module Base includes test classes and methods in /src/test/java They seem to have access to the IoC container; in other words, if a test method invokes Foo.findAllFoos() for entity class Foo, it works. I have no idea how the applicationContext is getting set up for the test classes, though; I am assuming that one of the Spring runtime classes knows to look for the declarative stuff in /src/main? Or is Junit doing some magic behind the scenes?
But now if I go to module Other, and there is a test class in /src/test/java that imports base.foo, and if a method invokes Foo.findAllFoos(), it will, not surprisingly, complain that the entity manager has not been injected. That makes sense, because nobody told anybody to activate the application context for module Base.
So what's a good way to deal with this? I don't think it seems wise for every single test class in module Other to set up the IoC container and then tear it down for every single test. But if I try to set something up declaratively, i.e. in /src/test/resources/META-INF/spring/application-context.xml, who is it whose job it it is to read that file and set up the application context?