在Arquillian测试中注入@Stateless EJB [英] Injecting @Stateless EJB in Arquillian tests

查看:72
本文介绍了在Arquillian测试中注入@Stateless EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行Arquillian测试时出现一个奇怪的问题.如果我尝试使用带有@Stateless注释的EJB,则会出现此错误:

A weird problem when running Arquillian test. If I try to use an EJB annotated with @Stateless, I get this error:

org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [MyEjbRemote] with qualifiers [@Default] at injection point [[field] @Inject private com.org.app.ejb.InjectionTest.ejb]

对于Arquillian,我具有以下测试类和部署:

I have the following test class + deployment for the Arquillian:

@RunWith(Arquillian.class)
public class InjectionTest extends TestCase {

  @Inject
  private MyEjbRemote ejb;

  @Deployment
  public static JavaArchive createDeployment() {
    JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "test.jar").addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"));
    jar.addClass(MyEjbRemote.class);
    jar.addClass(MyEjb.class);
  }

  @Test
  public void checkInjection() {
    TestCase.assertNotNull(ejb);
  }
}

EJB看起来像这样:

The EJB looks like this:

@Stateless
@Default
public class MyEjb implements MyEjbRemote {
  public MyEjb() {
  }
}

远程接口仅具有@Remote注释.

The remote interface just has @Remote annotation.

如果我将 @Stateless 更改为 @Named ,则可以使用.但是我想使用@Stateless.

If I change the @Stateless to @Named, it works. But I wan to use the @Stateless.

pom.xml:

<dependencies>
  <dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
  </dependency>
  <dependency>
    <groupId>org.jboss.arquillian.junit</groupId>
    <artifactId>arquillian-junit-container</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.jboss.arquillian</groupId>
      <artifactId>arquillian-bom</artifactId>
      <version>1.1.9.Final</version>
      <scope>import</scope>
      <type>pom</type>
    </dependency>
  </dependencies>
</dependencyManagement>

推荐答案

为了测试不是Singletons的无状态EJB的注入,您需要部署到嵌入式容器.

In order to test injection of Stateless EJBs that are not Singletons you need to deploy to an embedded container.

我遇到了同样的问题,这篇博客文章向我指出了正确的方向. https://blog.payara.鱼/如何使用Payara服务器与微型计算机测试应用程序

I had the same issue and this blog post pointed me in the right direction. https://blog.payara.fish/how-to-test-applications-with-payara-server-micro-with-arquillian

我的工作pom.xml

My working pom.xml

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>1.6.0.Final</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.container</groupId>
        <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
        <version>1.0.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>fish.payara.extras</groupId>
        <artifactId>payara-embedded-all</artifactId>
        <version>5.2020.4</version>
        <scope>test</scope>
    </dependency>
</dependencies>

这篇关于在Arquillian测试中注入@Stateless EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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