我正在尝试使用Weld SE 2.3.0.Final在测试期间交换注入依赖项的替代实现,方法是在beans.xml
中提供不同的src/test/resources/META-INF
虽然似乎总是使用beans.xml
的主要版本,但我不确定原因。
首先,这里是支持类
Engine.java
public interface Engine
{
void start();
void stop();
}
DefaultEngine.java
@Vetoed
public class DefaultEngine implements Engine
{
public void start()
{
System.out.println("Cough cough vrummmmm");
}
public void stop()
{
System.out.println("Phhhut clank");
}
}
Car.java
public class Car
{
@Inject
private Engine engine;
public void startCar()
{
engine.start();
}
public void stopCar()
{
engine.stop();
}
}
EngineProducer.java
public class EngineProducer
{
@Produces
public Engine getEngine()
{
return new DefaultEngine();
}
}
App.java
public class App
{
@Inject
private Car car;
public void main(@Observes ContainerInitialized event)
{
car.startCar();
car.stopCar();
}
}
beans.xml
src/main/resources/META-INF
的生产版本
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/beans_1_0.xsd">
</beans>
从App.java运行时的上述代码应该打印(并且确实如此)
Cough cough vrummmmm
Phhhut clank
现在从测试代码中我想使用SportsEngine
实现。所以我有以下
SportsEngine.java
@Vetoed
public class SportsEngine implements Engine
{
public void start()
{
System.out.println("Vrummm vrummm vrummmm!");
}
public void stop()
{
System.out.println("Prrrrrr clunk");
}
}
TestEngineProducer.java
@Alternative
public class TestEngineProducer
{
@Produces
public Engine getEngine()
{
return new SportsEngine();
}
}
AppTest.java
@RunWith(WeldJUnit4Runner.class)
public class AppTest
{
@Inject
private Car car;
@Test
public void testCar()
{
car.startCar();
car.stopCar();
}
}
WeldJUnit4Runner.java
public class WeldJUnit4Runner extends BlockJUnit4ClassRunner
{
/** The test class to run. */
private final Class<?> mKlass;
/** Weld infrastructure. */
private final Weld weld;
/** The container itself. */
private final WeldContainer container;
/**
* Runs the class passed as a parameter within the container.
*
* @param klass
* to run
* @throws InitializationError
* if anything goes wrong.
*/
public WeldJUnit4Runner(final Class<Object> klass) throws InitializationError
{
super(klass);
this.mKlass = klass;
this.weld = new Weld();
this.container = weld.initialize();
}
@Override
protected Object createTest() throws Exception
{
final Object test = container.instance().select(mKlass).get();
return test;
}
}
beans.xml
src/test/resources/META-INF
测试版
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/beans_1_0.xsd">
<alternatives>
<class>com.test.testing.TestEngineProducer</class>
</alternatives>
</beans>
运行测试方法时,应打印
Vrummm vrummm vrummmm!
Prrrrrr clunk
但相反它似乎使用EngineProducer
而不是TestEngineProducer
并打印输出两次
Cough cough vrummmmm
Phhhut clank
Cough cough vrummmmm
Phhhut clank
为什么不使用test beans.xml?
如果我从test beans.xml中放入<alternatives>
部分并将其放在main beans.xml中,那么它可以工作,但我不想在main / resources /中交换beans.xml我测试时的META-INF
答案 0 :(得分:2)
这似乎是Weld Se的一个错误。它只使用src / main / resources / META-INF下的beans.xml版本。
解决方案是使用CDI-SE src / main / resources / META-INF / beans.xml下的beans.xml进行测试,您可能需要添加&lt;排除&GT;进入beans.xml。
您可以拥有另一个生产beans.xml文件,并将代码导出到部署包(jar,war,ear)。不要忘记用production beans.xml替换测试beans.xml。
如果您正在开发网络应用程序。您可以在WEB-INF下创建生产beans.xml。存在beans.xml的2个版本,但WAS仅使用WEB-INF / beans.xml。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
<scan>
<exclude name="org.jboss.weld.**" />
<exclude name="com.your_package.EngineProducer" />
</scan>
<alternatives>
<class>com.test.testing.TestEngineProducer</class>
</alternatives>
</beans>