Weld itself cannot help you with this, Weld SE will in fact pick the first beans.xml
on classpath. To be more precise, main/resources/META-INF/beans.xml
will be used for bean archive main/java/
while test/resources/META-INF/beans.xml
willbe used for bean archive test/java/
. Therefore, mixing this up would be unwise at best and so Weld does not support it by default.
Anyhow, there are several options to solve your trouble.
Use Arquillian + Shrinkwrap
For CDI testing, this is the best way you can hope for and a very good one, once you learn it. Shrinkwrap will allow you to tailor the deployments exactly to your needs, including only the classes you want and also beans.xml
you want. There is an Arquillian container for Weld SE which is even used in Weld SE testsuite, so you can inspire yourself there.
Disable discovery while bootstrapping Weld SE
Somewhere in your unit tests, you are starting Weld container. While doing so, you might use Weld#disableDiscovery()
, which means you will create a synthetic bean archive. In such archive, default discovery is disabled and only things you specifically add (via methods such addBeanClasses
, addPackage
, ..) will land in the resulting archive. And since you ahve n odiscovery, no beans.xml
will be picked up! Instead, you define all the alternatives, interceptors, ... (again there are methods for this on the Weld
class). Now, I imagine this could be "easily" placed into some @Before
method should you need to do this repeatedly. Alternatively, you could also use Weld-junit
which will provide you with a JUnit @Rule
and allows you to easily describe the deployment on per class basis.
Both of the above approaches should help you with that hardships. But as I said, there is no way to achieve this with just plain Weld SE.