简体   繁体   English

使用src / test / META-INF / beans.xml中的替代生产者进行焊接

[英]Weld using alternative producer from src/test/META-INF/beans.xml

I am trying to use Weld SE 2.3.0.Final to swap an alternative implementation of an injected dependency during testing by supplying a different beans.xml in src/test/resources/META-INF 我试图通过提供不同的使用焊接SE 2.3.0.Final测试期间交换替代实现注入依赖beans.xmlsrc/test/resources/META-INF

It always seems to use the main version of beans.xml though and I'm not sure why. 虽然它似乎总是使用beans.xml的主要版本,但我不确定为什么。

First of all here are the supporting classes 首先是支持班

Engine.java Engine.java

public interface Engine
{
    void start();
    void stop();
}

DefaultEngine.java 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 Car.java

public class Car
{
    @Inject
    private Engine engine;

    public void startCar()
    {
        engine.start();
    }

    public void stopCar()
    {
        engine.stop();
    }

}

EngineProducer.java EngineProducer.java

public class EngineProducer
{

    @Produces
    public Engine getEngine()
    {
        return new DefaultEngine();
    }

}

App.java App.java

public class App 
{
    @Inject
    private Car car;

    public void main(@Observes ContainerInitialized event)
    {
        car.startCar();
        car.stopCar();
    }
}

And the production version of beans.xml in src/main/resources/META-INF 以及src/main/resources/META-INFbeans.xml的生产版本

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

The above code when run from App.java should print (and it does) 从App.java运行时,上面的代码应打印(并且可以)

Cough cough vrummmmm
Phhhut clank

Now from the test code I want to use the SportsEngine implementation. 现在,从测试代码中,我想使用SportsEngine实现。 So I have the following 所以我有以下

SportsEngine.java 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 TestEngineProducer.java

@Alternative
public class TestEngineProducer
{

    @Produces
    public Engine getEngine()
    {
        return new SportsEngine();
    }

}

AppTest.java AppTest.java

@RunWith(WeldJUnit4Runner.class)
public class AppTest
{

    @Inject
    private Car car;

    @Test
    public void testCar()
    {
        car.startCar();
        car.stopCar();
    }

}

WeldJUnit4Runner.java 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;
    }
}

And the test version of beans.xml in src/test/resources/META-INF 以及src/test/resources/META-INFbeans.xml测试版本

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

When the test method is run it should print 运行测试方法时,应打印

Vrummm vrummm vrummmm!
Prrrrrr clunk

But instead it seems to use the EngineProducer instead of the TestEngineProducer and prints the output twice 但是相反,它似乎使用了EngineProducer而不是TestEngineProducer并输出了两次输出

Cough cough vrummmmm
Phhhut clank
Cough cough vrummmmm
Phhhut clank

Why is it not using the test beans.xml? 为什么不使用测试bean.xml?

If I put the <alternatives> section from test beans.xml and put it in the main beans.xml then it works, but I don't want to swap the beans.xml in main/resources/META-INF when I'm testing 如果我将测试bean.xml中的<alternatives>部分放入主bean.xml中,则它可以工作,但是我不想在main / resources / META-INF中交换beans.xml测试

It seems a bug of Weld Se. 看来是Weld Se的错误。 It only use beans.xml version under src/main/resources/META-INF. 它仅使用src / main / resources / META-INF下的beans.xml版本。

The solution is using beans.xml under CDI-SE src/main/resources/META-INF/beans.xml for test purpose and you may need to add < exclude> into beans.xml too. 解决方案是使用CDI-SE src / main / resources / META-INF / beans.xml下的bean.xml进行测试,您可能还需要将<exclude>添加到beans.xml中。

You can have another production beans.xml file and when you export your code into deployment package (jar, war, ear). 在将代码导出到部署包(jar,war,ear)中时,可以有另一个生产bean.xml文件。 Don't forget to replace your testing beans.xml by your production beans.xml. 不要忘记用生产bean.xml替换测试bean.xml。

If you are developing web app. 如果您正在开发Web应用程序。 You can create your production beans.xml under WEB-INF. 您可以在WEB-INF下创建生产bean.xml。 2 version of beans.xml exists but only WEB-INF/beans.xml is used by WAS. 存在2个版本的beans.xml,但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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM