我正在尝试使用 Weld SE 2.3.0.Final 在测试期间通过提供不同beans.xml
的 in来交换注入依赖项的替代实现src/test/resources/META-INF
它似乎总是使用beans.xml
虽然的主要版本,我不知道为什么。
首先这里是支持类
引擎.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");
}
}
汽车.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();
}
}
应用程序.java
public class App
{
@Inject
private Car car;
public void main(@Observes ContainerInitialized event)
{
car.startCar();
car.stopCar();
}
}
beans.xml
和in的生产版本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
和in的测试版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
为什么不使用测试 beans.xml?
如果我将<alternatives>
测试 beans.xml 中的部分放在 main beans.xml 中,那么它可以工作,但是我不想在测试时交换 main/resources/META-INF 中的 beans.xml