Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unit testing EJBs with Maven 2 and Glassfish 3

I've been trying to set my application up so that I can unit test it's EJBs all day but I can't seem to get past what seems like a really simple problem.

I have a standard Maven web application set up in NetBeans 6.9. I've automatically generated a unit test for one of the EJBs but whenever I go to run it I get the error message:

Testcase: initializationError(com.example.ExampleTest): Caused an ERROR
Absent Code attribute in method that is not native or abstract in class file javax/ejb/embeddable/EJBContainer
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ejb/embeddable/EJBContainer
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)

I've researched this one to death and I'm pretty sure the problem is that my pom currently points to a jar which contains only the API

<dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

Not and implementation that can be used for running the tests. I'm pretty sure the test is failing in the method marked with @BeforeClass when it tries to execute

container = EJBContainer.createEJBContainer();

The standard recommended solution is to add the glassfish-embedded-all artifact as the first project dependency with test scope

<dependency>
            <groupId>org.glassfish.extras</groupId>
            <artifactId>glassfish-embedded-all</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>

I can find a Maven package for this artifact here: http://download.java.net/maven/glassfish/ but Nexus doesn't recognize this directory or any of the subdirectories as Maven 2 repositories. I suppose I could download the jar and manually install it into Nexus but that seems to defeat the who point of installing Nexus.

So, is there a Maven repository that Nexus will be able to index to give me the glassfish-embedded-all artifact? A couple of posts I read mentioned that the correct artifact to now be using is javax.ejb but I've had no more luck finding this.

As you've probably guessed I'm completely new to unit testing and fairly new to JEE6 in general; is this even the correct way to go about unit testing EJBs?

like image 923
wobblycogs Avatar asked Aug 05 '10 14:08

wobblycogs


1 Answers

(...) The standard recommended solution is to add the glassfish-embedded-all artifact as the first project dependency with test scope

Indeed, you need an implementation like glassfish-embedded-all or glassfish-embedded-web if you're only using the web profile, which seems to be your case (I didn't know that the web profile was providing EJBContainer by the way).

And to be precise, this artifact doesn't have to be the "first" dependency but it must be declared before the javaee-api artifact.

So, is there a Maven repository that Nexus will be able to index to give me the glassfish-embedded-all artifact?

I couldn't reproduce the issue with http://download.java.net/maven/glassfish/ but it appears that JBoss Nexus repository does have it (probably because they use it in Arquillian):

<repository>
  <id>jboss-public-repository-group</id>
  <name>JBoss Public Maven Repository Group</name>
  <url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>

is this even the correct way to go about unit testing EJBs?

Unit tests are typically done outside-container and in isolation (using a Mocking framework) so I wouldn't call that unit testing. But for integration/functional testing (in-container), the EJBContainer API is really great and perfectly fine.

See also

  • TOTD #128: EJBContainer.createEJBContainer: Embedded EJB using GlassFish v3
  • Unit Testing EJBs and JPA with Embeddable GlassFish
  • Using the EJBContainer API with or without Maven (but with GlassFish v3)
  • Embedding EJB 3.1 Container Into Your Unit Tests - Boot Time: 5 Seconds
  • Unit Testing EJB 3.1 ...When 0.8 Seconds Are Too Long for a unit test example
  • The Arquillian project
like image 142
Pascal Thivent Avatar answered Oct 20 '22 20:10

Pascal Thivent