CDI not working with Spock and maven-generated EAR
karomann May 17, 2013 3:37 PMHi,
I tried to run Arquillian with Spock on JBoss standalone 7.1.3. The archive seems to be deployed (from what I see in JBoss logs), but no dependencies get injected - not even EntityManger. I tried to follow the examples from https://github.com/arquillian/arquillian-testrunner-spock/tree/master/examples. I am trying to deploy my specification with a pre-prepared maven artifact. Here is my configuration:
pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.7.0-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>1.8.6-01</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<!-- ... -->
<!-- Arquillian -->
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-depchain</artifactId>
<type>pom</type>
<scope>test</scope>
<version>2.0.0-beta-2</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
<version>1.0.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-arquillian-container-remote</artifactId>
<scope>test</scope>
<version>7.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<scope>test</scope>
<version>1.0.3.Final</version>
</dependency>
<!-- Spock -->
<dependency>
<groupId>org.jboss.arquillian.spock</groupId>
<artifactId>arquillian-spock-container</artifactId>
<scope>test</scope>
<version>1.0.0.Alpha2</version>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
<version>0.6-groovy-1.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<scope>test</scope>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
arquillian.xml:
<arquillian xmlns="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<defaultProtocol type="Servlet 3.0" />
<container qualifier="jboss7" default="true">
<configuration>
<property name="managementAddress">127.0.0.1</property>
<property name="managementPort">9999</property>
</configuration>
</container>
</arquillian>
Finally specification itself:
class MyClassSpec extends Specification {
@EJB
MyClass testedStuff;
@Inject
UserTransaction utx;
@PersistenceContext(unitName="mcore_pu")
EntityManager em;
@Inject
DevicesUtil devUtil;
ExternalSystem mockExternalSystem
Devices orderer
@Deployment
//@OverProtocol("Servlet 3.0")
def static EnterpriseArchive deploy() {
def ear = DeploymentHelper.getApplicationEar(MyClass.class);
//ear.addAsResource(EmptyAsset.INSTANCE, "beans.xml");
return ear;
}
def "my feature, happy flow"() {
when:
testedStuff.doSomething(orderer);
then:
1 * mockExternalSystem.someMethod(_,_,_,_,_)
}
@Override
def setup() {
println("setup")
mockExternalSystem= Mock(ExternalSystem)
testedStuff.setExternalSystem(mockExternalSystem)
dev = devUtil.createDummyDevice();
utx.begin()
em.persist(dev);
utx.commit()
}
}
And that's how the EAR is prepared after being generated with Maven (this code works perfectly in Arquillian + JUnit):
public static EnterpriseArchive getApplicationEar(Class<?> testClass) {
EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class, "application-ear.ear")
.as(ZipImporter.class)
.importFrom(new File("../application-ear/target/application-ear-1.0-SNAPSHOT.ear"))
.as(EnterpriseArchive.class);
//now add the testClass and any test util classes that are not in the archive
JavaArchive testLibraryHelper = ShrinkWrap.create(JavaArchive.class)
.addClass(testClass)
.addPackage(MiscUtil.class.getPackage())
.addPackage(some.other.UtilClass.class.getPackage())
//now for CDI working in testLibraryHelper
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
ear.addAsLibrary(testLibraryHelper);
return ear;
}
Any help greatly appreciated