Search code examples
javaglassfishintegration-testingjboss-arquillianpayara

EAR integration test with arquillian


I have the following real project structure:

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

I want to test it with arquillian but I always get an exception.

This is my java method where the EAR is built:

@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;
}

The content looks fine but something is not okay because I get this exception:

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

JAR content:

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

EAR content

/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

Solution

  • The reason why I got "ArquillianServletRunner not found" error was because there was an error on EJB level.

    The following test code works properly:

    @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);
        }
    }
    

    used maven dependencies:

    <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>