显式本地EJB未使用Arquillian注入

Ral*_*lph 8 java testing java-ee java-ee-6 jboss-arquillian

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

@Stateless
public class TestServiceImpl implements TestServiceLocal, TestServiceRemote {
    public String greet() {
        return "hallo";
    }
}
Run Code Online (Sandbox Code Playgroud)

远程接口:

@Remote
public interface TestServiceRemote {
    public String greet();
}
Run Code Online (Sandbox Code Playgroud)

语言环境界面:

@Local
public interface TestServiceLocal {
    public String greet();
}
Run Code Online (Sandbox Code Playgroud)

这是测试:

@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);
    }    
}
Run Code Online (Sandbox Code Playgroud)

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

    <!-- 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>
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)

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

Vin*_*lds 13

您可以使用以下之一:

  • beans.xml文件添加到部署:

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

    这使得Arquillian的CDITestEnricher成为可能,它比EJBTestEnricher更强大.它可以处理@Inject注解(明显),但也@Resource@EJB注释,以及(参见CDI规范上的资源注入的部分).然后,容器将@EJB测试类实例中的带注释字段视为注入点并注入依赖项.

  • 使用已部署Bean的可移植JNDI名称为该字段指定注释的mappedName属性@EJB.在您的情况下,它看起来像:

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

    您需要确保可移植JNDI名称与为部署生成的名称相同.我只是指定了为我的"com.acme.TestServiceLocal"界面生成的那个.


Poo*_*ool 5

除了答案,您还必须确保使用正确的@Deployment设置是正确的.要在本地注入,您必须确保拥有@Deployment(testable=true)(请注意,这是默认设置).

来自Aquillian的文档:

容器内模式:@Deployment(testable = true)

正如我们上面提到的,我们需要重新打包你的@Deployment,添加一些Arquillian支持类,以便在容器中运行.这使我们能够与测试通信,丰富测试并远程运行测试.在此模式下,测试在远程容器中执行; Arquillian默认使用此模式.

有关提供@Deployment时打包过程的预期输出的概述,请参阅"完整协议参考".

客户端模式:@Deployment(testable = false)

现在这种模式很容易.与重新打包和覆盖测试执行的容器内模式相反,as-client模式尽可能少.它不会重新打包您的@Deployment,也不会将测试执行转发到远程服务器.您的测试用例正在您的JVM中按预期运行,您可以自由地从外部测试容器,就像您的客户看到的那样.Arquillian唯一能做的就是控制@Deployment的生命周期.

以下是使用as client模式调用Servlet的示例.

这对OP没有帮助(他们有正确的设置),但我希望它可以帮助那些来自Google的人,就像我一样.