【问题标题】:Arquillian @EJB injecting null on EAR deploymentArquillian @EJB 在 EAR 部署中注入 null
【发布时间】:2014-08-09 13:41:04
【问题描述】:

我一直在尝试在我的“远程”本地 JBoss 服务器(非嵌入式/托管)上执行 Arquillian 集成测试,但我的本地 EJB 没有被注入。

pom.xml

<profile>
    <!-- An optional Arquillian testing profile that executes tests in a remote JBoss AS instance -->
    <!-- Run with: mvn clean test -Parq-jbossas-remote -->
    <id>arq-jbossas-remote</id>
    <dependencies>
        <dependency>
            <groupId>org.jboss.as</groupId>
            <artifactId>jboss-as-arquillian-container-remote</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</profile>

ProductService.java:

@Stateless
@Local(IProductService.class)
public class ProductService implements IProductService {

ProductServiceIntegrationTest.java

@RunWith(Arquillian.class)
public class ProductServiceIntegrationTest {

    protected static final Logger logger = LoggerFactory.getLogger(ProductServiceIntegrationTest.class);

    @Rule
    public TestName testMethod = new TestName();

    @EJB
    public IProductService productService;

    //    @Deployment(order = 1, name = "keystone-ear", testable = false)
    //    public static Archive<?> createKeystoneServicesArchive() {
    //        return ShrinkWrap
    //                .create(ZipImporter.class, "keystone-ear.ear")
    //                .importFrom(new File("target/dependency/keystone-ear.ear"))
    //                .as(EnterpriseArchive.class);
    //    }
    //    
    //    @Deployment(order = 2, name = "stock-wiz-ear", testable = false)
    //    public static Archive<?> createStockWizEjbArchive() {
    //        return ShrinkWrap
    //                .create(ZipImporter.class, "stock-wiz-ear.ear")
    //                .importFrom(new File("target/dependency/stock-wiz-ear.ear"))
    //                .as(EnterpriseArchive.class);
    //    }

    @Deployment
    public static Archive<?> createConductorEjbArchive() {

        JavaArchive ejbModule = ShrinkWrap
                .create(ZipImporter.class, "conductor-ejb.jar")
                .importFrom(new File("target/dependency/conductor-ejb.jar"))
                .as(JavaArchive.class);

        ejbModule.deletePackage("za.co.fnb.cbs.conductor.component.camel");

        EnterpriseArchive ear = ShrinkWrap
                .create(ZipImporter.class, "conductor-ear.ear")
                .importFrom(new File("target/dependency/conductor-ear.ear"))
                .as(EnterpriseArchive.class);
        ear.delete("/conductor-ejb.jar");
        ear.delete("/controller-web.war");
        ear.addAsModule(ejbModule);
        System.out.println(ear.toString(true));
        return ear;
    }

    @Test
    public void shouldBeInjected() throws Exception {
        Assert.assertNotNull(productService);
    }

}

我已经注释掉了两个附加部署,它们是我的集成测试的依赖项。在执行测试之前,我手动将依赖项部署到服务器。无论如何,我似乎无法让它工作。

我还尝试如下指定 JNDI 映射名称:

@EJB(mappedName = "java:global/conductor/conductor-ejb/ProductService!za.co.fnb.cbs.conductor.api.smartdevices.services.IProductService")
public IProductService productService;

我也尝试过通过上下文查找 EJB...

@Before
public void before() throws Exception {
    Properties jndiProps = new Properties();
    jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
//            env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");
    jndiProps.put(Context.PROVIDER_URL, "remote://localhost:4447");
    initialContext = new InitialContext(jndiProps);
    productService = (IProductService)initialContext.lookup("java:module/ProductService!za.co.fnb.cbs.conductor.api.smartdevices.services.IProductService");
}

我也尝试过 CDI 注入,并将 beans.xml 放在测试资源类路径以及主类路径中。没有运气。

Arquillian 是否能够在 EnterpriseArchive 上运行集成测试? 根据我的经验,情况似乎并非如此。

我遇到的所有示例都涉及创建一个带有一两个类的小型 JavaArchive。这对我不起作用。由于需要所有依赖项,我必须部署 EAR。

【问题讨论】:

标签: maven jakarta-ee integration-testing jboss-arquillian


【解决方案1】:

EJB 是一个托管 bean。如果它的 null 意味着它没有被注入,一个可能的原因可能是 JMX 超时,这意味着 MBean 服务器没有及时响应请求,我要说的是增加 JMX 超时并给它一个 bash xxxxxxx。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多