Daniel Cerecedo
Daniel Cerecedo

Reputation: 6217

How to test Spring in an application server container with Arquillian?

We are trying to run integration tests of Spring based code on an embedded tomcat container using TestNG framework.

We have tried to use the existing Arquillian Spring Extension without much success. Maybe some missing configuration. We have followed instructions from this post

Our pom includes the following dependencies:

        <dependency>
        <groupId>org.jboss.arquillian</groupId>
        <artifactId>arquillian-bom</artifactId>
        <version>1.0.2.Final</version>
        <scope>test</scope>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.testng</groupId>
        <artifactId>arquillian-testng-container</artifactId>
        <version>1.0.2.Final</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
        <scope>test</scope>
    </dependency>
    <!-- Spring Extension -->
    <dependency>
        <groupId>org.jboss.arquillian.extension</groupId>
        <artifactId>arquillian-service-container-spring</artifactId>
        <version>1.0.0.Beta1</version>
        <scope>test</scope>
    </dependency>
    <!-- testing END -->

We have also added a Tomcat embedded profile to our pom.

        <profile>
        <id>arquillian-tomcat-embedded-7</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian.container</groupId>
                <artifactId>arquillian-tomcat-embedded-7</artifactId>
                <version>1.0.0.CR3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
                <version>7.0.30</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <version>7.0.30</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-juli</artifactId>
                <version>7.0.30</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.jdt.core.compiler</groupId>
                <artifactId>ecj</artifactId>
                <version>3.7</version>
                <scope>test</scope>
            </dependency>

            <!-- Weld servlet for testing CDI injections -->
            <dependency>
                <groupId>org.jboss.weld.servlet</groupId>
                <artifactId>weld-servlet</artifactId>
                <version>1.1.9.Final</version>
            </dependency>
        </dependencies>
    </profile>

Our arquillian.xml looks like...

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/schema/arquillian"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

<container qualifier="tomcat" default="true">
    <configuration>
        <property name="unpackArchive">true</property>
    </configuration>
</container>

We also tried adding the following lines to our arquillian.xml

<extension qualifier="spring">
    <property name="autoPackage">true</property>
    <property name="springVersion">3.0.0.RELEASE</property>
    <property name="cglibVersion">2.2</property>

    <property name="includeSnowdrop">true</property>
    <property name="snowdropVersion">2.0.3.Final</property>

    <property name="customContextClass">org.jboss.spring.vfs.context.VFSClassPathXmlApplicationContext</property>
</extension>

We have created an empty test and check that the embedded Tomcat starts and the test is run. The next step is to verify that the Arquillian Spring Extension is working properly, and to do so we are trying to inject a Spring bean into our test.

The problem is that the @SpringConfiguration annotation isn't available at all. So we guess we are missing some configuration. Any clues on how to proceed?

Upvotes: 1

Views: 4422

Answers (1)

Aslak Knutsen
Aslak Knutsen

Reputation: 726

You're using the arquillian-service-container-spring artifact which is the Embedded Spring container. To use if with another Container you need to use the arquillian-service-deployer-spring-3 and arquillian-service-integration-spring-inject artifacts.

service-deployer adds auto adding Spring dependencies to the deployment while service-integration-spring-inject adds the @Inject/@Autowire support in the test case.

See https://github.com/arquillian/arquillian-showcase/blob/master/spring/spring-inject/pom.xml#L43

Multiple other examples can be found here: https://github.com/arquillian/arquillian-showcase/tree/master/spring

Upvotes: 3

Related Questions