1

我有以下真实的项目结构:

EAR
 - lib/commons.jar
 - lib/another dependencies
 - ejb.jar

我想用 arquillian 测试它,但我总是遇到异常。

这是我构建 EAR 的 java 方法:

@Deployment
public static Archive<?> createTestArchive() {
    //create ear
    EnterpriseArchive ear = ShrinkWrap
            .create(EnterpriseArchive.class, "test-app.ear");

    // create ejb-jar
    JavaArchive ejb = ShrinkWrap
            .create(JavaArchive.class, "test-ejb.jar")
            .addPackage("a.b.ejb")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

    // resolve ejb dependencies
    File[] dependencies = Maven.resolver()
            .loadPomFromFile("pom.xml")
            .importDependencies(ScopeType.COMPILE, ScopeType.TEST)
            .resolve()
            .withTransitivity()
            .asFile();

    ear.addAsModule(ejb);
    ear.addAsLibraries(dependencies);
    ear.setApplicationXML("glassfish-resources.xml");

    LOGGER.debug("content: " + ear.toString(true));
    return ear;
}

内容看起来不错,但有些地方不行,因为我得到了这个异常:

ArquillianServletRunner not found.
Could not determine ContextRoot from ProtocolMetadata, please contact DeployableContainer developer.

JAR 内容:

/a/
/a/b/
/a/b/ejb/
/a/b/ejb/MyEjb.class
...
/META-INF/
/META-INF/beans.xml

EAR 内容

/test-ejb.jar
/lib/
/lib/commons-444750341265461918.jar
/lib/slf4j-api-1.7.21.jar
/lib/slf4j-log4j12-1.7.21.jar
/lib/guava-21.0.jar
...
/lib/arquillian-testenricher-initialcontext-1.1.13.Final.jar
/lib/payara-embedded-all-4.1.1.171.0.1.jar
/lib/postgresql-42.0.0.jar
/META-INF/
/META-INF/application.xml
4

1 回答 1

0

我收到“ ArquillianServletRunner not found”错误的原因是因为 EJB 级别存在错误。

以下测试代码正常工作:

@RunWith(Arquillian.class)
public class EchoServiceBeanTest {

    @EJB
    private EchoService echoService;

    @Deployment
    public static Archive<?> createDeployment() {
        // create ear
        EnterpriseArchive ear = ShrinkWrap
                .create(EnterpriseArchive.class, "test-app.ear");

        // create ejb.jar
        JavaArchive ejb = ShrinkWrap
                .create(JavaArchive.class, "test-ejb.jar")
                .addPackages(true, "a.b.ejb")
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
        LOGGER.debug("EJB content: " + ejb.toString(true));

        // build ear
        ear.addAsModule(ejb);

        LOGGER.debug("EAR deployment content: " + ear.toString(true));
        return ear;
    }

    @Test
    public void echo() throws Exception {
        Assert.assertNotNull(echoService);
        String expected = "hello";
        String returned = echoService.echo(expected);
        Assert.assertEquals(expected, returned);
    }
}

使用 Maven 依赖项:

<dependencies>
    <!-- provided jars -->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <!-- junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>
    <!-- arquillian -->
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <version>1.1.13.Final</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.container</groupId>
        <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
        <version>1.0.0.Final</version>
        <scope>test</scope>
    </dependency>
    <!-- embedded EE container -->
    <dependency>
        <groupId>fish.payara.extras</groupId>
        <artifactId>payara-embedded-all</artifactId>
        <version>4.1.1.171.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
于 2017-04-22T20:44:27.747 回答