显式本地EJB未使用Arquillian注入 [英] Explicite Local EJB not injected with Arquillian

查看:93
本文介绍了显式本地EJB未使用Arquillian注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Arquillian来测试具有显式本地和远程接口的无状态会话bean。但在测试中,Arquillian不会在具有本地接口类型的字段中注入任何内容,但它适用于远程接口。

I use Arquillian to test an stateless session bean that has an explicit local and remote interface. But in the test Arquillian does not "inject" anything in a field that has the type of the local interface, but it works for the remote interface.

@Stateless
public class TestServiceImpl implements TestServiceLocal, TestServiceRemote {
    public String greet() {
        return "hallo";
    }
}

远程接口:

@Remote
public interface TestServiceRemote {
    public String greet();
}

语言环境界面:

@Local
public interface TestServiceLocal {
    public String greet();
}

这是测试:

@RunWith(Arquillian.class)
public class GenericEntityDaoEjbIntegrationTest {

    @Deployment
    public static JavaArchive createTestArchive()
                  throws UnsupportedEncodingException {
        return ShrinkWrap.create(JavaArchive.class, "test.jar")
                .addClasses(
                        TestServiceLocal.class,
                        TestServiceRemote.class,
                        TestServiceImpl.class);
    }

    @EJB
    private TestServiceLocal testServiceLocal;

    @EJB
    private TestServiceRemote testServiceRemote;

    //Will Fail
    @Test
    public void testTestServiceLocal() {
        assertNotNull(this.testServiceLocal);
    }

    //Success    
    @Test
    public void testTestServiceRemote() {
        assertNotNull(this.testServiceRemote);
    }    
}

我使用的是arquillian-glassfish-embedded 1.0.0 .CR2,glassfish-embedded-all 3.1和arquillian-junit-container 1.0.0.CR5我的pom的相关部分是:

I am using arquillian-glassfish-embedded 1.0.0.CR2, glassfish-embedded-all 3.1 and arquillian-junit-container 1.0.0.CR5 The relevant part of my pom is:

    <!-- arquillian test -->
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <version>1.0.0.CR5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.container</groupId>
        <artifactId>arquillian-container-spi</artifactId>
        <version>1.0.0.CR5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.container</groupId>
        <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
        <version>1.0.0.CR2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1</version>
        <scope>test</scope>
    </dependency>

这是日志文件的相关部分(它不包含任何异常):

This is the relevant part of the log file (it does not contain any exception):

10.04.2012 15:38:16 com.sun.ejb.containers.BaseContainer initializeHome
INFO: Portable JNDI names for EJB TestServiceImpl : [java:global/test/TestServiceImpl!de.test.service.TestServiceRemote, java:global/test/TestServiceImpl!de.test.service.TestServiceLocal]
10.04.2012 15:38:16 com.sun.ejb.containers.BaseContainer initializeHome
INFO: Glassfish-specific (Non-portable) JNDI names for EJB TestServiceImpl : [de.test.service.TestServiceRemote, de.test.service.TestServiceRemote#de.test.service.TestServiceRemote]
10.04.2012 15:38:16 com.sun.enterprise.web.WebApplication start
INFO: WEB0671: Loading application [test] at [/test]
10.04.2012 15:38:16 org.glassfish.deployment.admin.DeployCommand execute
INFO: test was successfully deployed in 11.844 milliseconds.

我的错误是什么?我还需要更改为locale接口注入的get实例吗?

What is my mistake? What do I need to change the get an instance injected for locale interface too?

推荐答案

您可以使用以下方法之一:

You could use one of the following:


  • 在部署中添加 beans.xml 文件:

return ShrinkWrap.create(JavaArchive.class, "test.jar")
                .addClasses(
                        TestServiceLocal.class,
                        TestServiceRemote.class,
                        TestServiceImpl.class)
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

这启用了Arquillian的CDITestEnricher,它比EJBTestEnricher功能强大得多。它可以处理 @Inject 注释(显然),还可以 @Resource @EJB 注释(请参阅CDI规范中有关资源注入的部分)。然后容器将测试类实例中的 @EJB 注释字段视为注入点并注入依赖项。

This enables the CDITestEnricher of Arquillian, which is far more capable than the EJBTestEnricher. It can handle @Inject annotations (obviously), but also @Resource and @EJB annotations as well (see the section on Resources injection in the CDI spec). The container then treats both the @EJB annotated fields in your test class instance as injection points and injects the dependencies.

为具有已部署的可移植JNDI名称的字段的 @EJB 注释指定 mappedName 属性豆。在您的情况下,它看起来像:

Specify the mappedName property for the @EJB annotation for the field with the portable JNDI name of the deployed bean. In your case, it will look something like:

@EJB(mappedName="java:global/test/TestServiceImpl!com.acme.TestServiceLocal")
private TestServiceLocal testServiceLocal;

您需要确保便携式JNDI名称与为您生成的名称相同部署。我只是指定了为我的com.acme.TestServiceLocal界面生成的那个。

You'll need to ensure that the portable JNDI name is the same as that the one generated for your deployment. I've merely specified the one that was generated for my "com.acme.TestServiceLocal" interface.

这篇关于显式本地EJB未使用Arquillian注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆