Arquillian @EJB在EAR部署上注入null

时间:2014-06-19 05:38:46

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

我一直试图在我的"遥控器上执行Arquillian集成测试" localhost JBoss服务器(未嵌入/管理),但我的本地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,因为需要所有依赖项。

1 个答案:

答案 0 :(得分:0)

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