unit testing EJBs with Maven 2 and Glassfish 3
Asked Answered
H

1

11

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?

Harty answered 5/8, 2010 at 14:35 Comment(0)
H
13

(...) 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

Homophonous answered 5/8, 2010 at 15:33 Comment(6)
Thanks Pascal, an excellent answer as usual. Unfortunately it's not fixed my problem. I think I must be configuring Nexus incorrectly or something. I've tried setting up the repository you suggested and this one repository.jboss.org/nexus/content/repositories/glassfish and neither ever show anything to download under the "browse index" tab (if you're familiar with Nexus). I've got 4 or 5 other maven repositories configured fine under Nexus so I can't understand why these ones are proving so much trouble. I've even tried setting up a completely clean install of Nexus - still no joy.Harty
@Harty Regarding download.java.net/maven/glassfish, note that this repository doesn't provide a Nexus index (a .index directory at the root) so I wouldn't expect to see anything in the browse index tab if Nexus didn't download an artifact from it. However, repository.jboss.org/nexus/content/groups/public definitely does. But did you enable remote repository index downloads? See sonatype.com/books/nexus-book/reference/…. Anyway, does the proxying feature actually work or not (does your maven client succeed in downloading artifacts)?Homophonous
@pascal Yes, index downloads are enabled, in the Nexus logs I get: Aborted walking on repository ID='jboss-glassfish' from path='/.index', cause: Item not found on path "/.index" in repository "jboss-glassfish"!. No, I can't seem to download any of the artifacts from that repository. I tried adding the repository to my settings.xml but that didn't help either I simply got: Unable to find resource 'org.glassfish.extras:glassfish-embedded-all:jar:3.0.1' in repository jboss-glassfish (repository.jboss.org/nexus/content/repositories/glassfish) - odd since that URL is correct.Harty
@Harty Well, I'm afraid I won't be able to help further as I can't reproduce the issue. I just tried (with both repository.jboss.org/nexus/content/repositories/glassfish and repository.jboss.org/nexus/content/groups/public) and it works for me.Homophonous
For anyone reading this question I'm happy to report that I've got past this problem and I've actually run some tests. The main problem seemed to be Nexus refusing to look for the glassfish-embedded-all artifact. By just fiddling with it I managed to get it to download it but it broke a number of other repositories which I hadn't touch. Eventually I gave up and re-setup Nexus from scratch. All artifacts are downloading fine now, I can only assume the Nexus repository became corrupted somehow.Harty
Thanks a bunch. Searched a lot until I found yours!Bounder

© 2022 - 2024 — McMap. All rights reserved.