0

we face against the same problem than the one identified here : Weld using alternative producer from src/test/META-INF/beans.xml.

With maven and weld se, for test, weld does not use the beans.xml located in test/resources/META-INF.

We look on Google, and i the Jira issues of Weld, but it seems that others people do not have the same problem than us.

So does someone know how to use weld in Junit test with Maven and to achieve that Weld SE is configured by the beans.xml located in test/resources/META-INF ?

Community
  • 1
  • 1
granier
  • 1,739
  • 13
  • 25

1 Answers1

0

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.

  1. 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.

  2. 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.

Siliarus
  • 6,393
  • 1
  • 14
  • 30
  • Hi, thanks for the answer and we will try Arquillian + Shrinkwrap. But our point that when we run maven test (id test/java/ ) , the file test/resources/META-INF/beans.xml is not use and this is the one in main/resources/META-INF which is used. – granier Feb 21 '17 at 14:24
  • Yea, what I am saying is, there is no way to tell Weld "Hey, pick this one!". Instead, I am offering you two alternative ways to do what you want. – Siliarus Feb 21 '17 at 14:35