Arquillian Glassfish - Managed or Embedded?
hpropes Nov 20, 2015 1:59 PMHi,
In production, I'm writing Java EE Web Applications using Maven 2, Glassfish 4.1, and Netbeans 8. For testing, I'd like to use Arquillian with a Glassfish environment (either embedded or managed, I need guidance). Yesterday I followed the tutorial at Getting Started · Arquillian Guides, adapting it to my own setup.
I did not use the maven archetype or JForge. I simply attempted to add the Arquillian dependencies to my own project's pom.xml.
Here are the Arquillian-related dependencies in my pom.xml:
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-7.0</artifactId>
<version>1.0.3.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.10.Final</version>
<scope>provided</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.1.10.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap</groupId>
<artifactId>shrinkwrap-api</artifactId>
<version>1.2.2</version>
<type>jar</type>
</dependency>
<profiles>
<profile>
<id>arquillian-glassfish-embedded</id>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.0.CR4</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
These dependencies are from a parent pom.xml that I am required to use:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>4.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.sun</groupId>
<artifactId>tools-jar</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>activation</artifactId>
<groupId>javax.activation</groupId>
</exclusion>
<exclusion>
<artifactId>javax.mail</artifactId>
<groupId>com.sun.mail</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
<optional>${skinny.war}</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.12</version>
<optional>${skinny.war}</optional>
</dependency>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.code.version}</source>
<target>${java.code.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/test/**</include>
</includes>
</configuration>
</plugin>
</plugins>
I wrote a test file that looks like this:
/**
*
* @author hpropes
*/
public class SampleTest
{
@Deployment
public static JavaArchive createDeployment() {
JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
.addClass(SampleBean.class)
.addAsManifestResource("META-INF/persistence.xml", "persistence.xml")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
System.out.println(jar.toString(true));
return jar;
}
@Inject private SampleBean sample;
@Test
public void showSample(){
Assert.assertEquals(5, sample.getMyValue());
System.out.println(String.format("sample myValue is %d", sample.getMyValue()));
}
}
Which tests this bean:
@Model
public class SampleBean
{
private @Getter final int myValue = 5;
}
I run this test and I get a NullPointerException on this line of my test class:
Assert.assertEquals(5, sample.getMyValue());
Apparently CDI in my test class is not working.
Can anyone help with this? I've heard rumors that I can also use arquillian-glassfish-managed instead of arqueillian-glassfish-embedded. What is the difference between the two? Is there a complete tutorial for setting up Arquillian in a modern Glassfish/Java EE/Netbeans/Maven environment?:
Any suggestions would be GREAT.
thanks, Heather