users@glassfish.java.net

Test EJB app with JUnit and maven-embedded-glassfish-plugin

From: <glassfish_at_javadesktop.org>
Date: Mon, 18 Oct 2010 09:50:14 PDT

Hi all,

I have a problem running JUnit tests for an EJB app. I created a very simple test application that demonstrates my problem.

[b]My EJB remote interface:[/b]

package testea.server;

import javax.ejb.Remote;

@Remote
public interface MagicBoxRemote {

        int getMeDaNumber();

}

[b]My EJB:[/b]

package testea.server;

import javax.ejb.Stateless;

@Stateless
public class MagicBoxBean implements MagicBoxRemote {

        @Override
        public int getMeDaNumber() {
                return 42;
        }
}

[b]My JUnit test:[/b]

package testea.server;

import java.util.HashMap;
import java.util.Map;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import junit.framework.Assert;

import org.junit.Test;

public class MagicBoxTest {
        public Object lookupBean(final String beanName) throws NamingException {
                return new InitialContext().lookup("java:global/TestEA/" + beanName); //$NON-NLS-1$
        }

        @Test
        public void testDaNumber() throws NamingException {
                MagicBoxRemote magicBox = (MagicBoxRemote) lookupBean("MagicBoxBean");
                Assert.assertEquals(42, magicBox.getMeDaNumber());
        }
}

[b]My pom.xml:[/b]
<project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <modelVersion>4.0.0</modelVersion>
        <groupId>testea</groupId>
        <artifactId>server</artifactId>
        <packaging>ejb</packaging>
        <version>0.0.1</version>

        <build>
                <finalName>TestEA</finalName>
                <sourceDirectory>ejbModule</sourceDirectory>
                <testSourceDirectory>test</testSourceDirectory>
                <plugins>
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-compiler-plugin</artifactId>
                                <configuration>
                                        <source>1.6</source>
                                        <target>1.6</target>
                                </configuration>
                        </plugin>
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-ejb-plugin</artifactId>
                                <configuration>
                                        <ejbVersion>3.0</ejbVersion>
                                </configuration>
                        </plugin>
                        <plugin>
                                <groupId>org.glassfish</groupId>
                                <artifactId>maven-embedded-glassfish-plugin</artifactId>
                                <version>3.0.1</version>
                                <configuration>
                                        <goalPrefix>embedded-glassfish</goalPrefix>
                                        <app>${basedir}/target/classes</app>
                                        <autoDelete>true</autoDelete>
                                        <port>8080</port>
                                </configuration>

                                <executions>
                                        <execution>
                                                <id>start-glassfish</id>
                                                <phase>pre-integration-test</phase>
                                                <goals>
                                                        <goal>start</goal>
                                                </goals>
                                        </execution>
                                        <execution>
                                                <id>glassfish-deploy</id>
                                                <phase>pre-integration-test</phase>
                                                <goals>
                                                        <goal>deploy</goal>
                                                </goals>
                                        </execution>
                                        <execution>
                                                <id>glassfish-undeploy</id>
                                                <phase>post-integration-test</phase>
                                                <goals>
                                                        <goal>undeploy</goal>
                                                </goals>
                                        </execution>
                                        <execution>
                                                <id>stop-glassfish</id>
                                                <phase>post-integration-test</phase>
                                                <goals>
                                                        <goal>stop</goal>
                                                </goals>
                                        </execution>
                                </executions>
                        </plugin>
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-surefire-plugin</artifactId>
                                <configuration>
                                        <skip>true</skip>
                                </configuration>
                                <executions>
                                        <execution>
                                                <id>surefire-it</id>
                                                <phase>integration-test</phase>
                                                <goals>
                                                        <goal>test</goal>
                                                </goals>
                                                <configuration>
                                                        <skip>false</skip>
                                                </configuration>
                                        </execution>
                                </executions>
                        </plugin>
                </plugins>
        </build>
        <dependencies>
                <dependency>
                        <groupId>org.glassfish</groupId>
                        <artifactId>javax.javaee</artifactId>
                        <version>3.0-b54</version>
                        <scope>provided</scope>
                </dependency>
                <!-->dependency> <groupId>org.glassfish.extras</groupId> <artifactId>glassfish-embedded-all</artifactId>
                        <version>3.1-SNAPSHOT</version> </dependency -->
                <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.8.1</version>
                        <type>jar</type>
                        <scope>test</scope>
                </dependency>
        </dependencies>
</project>

When I try to run this, the test fails with the following exception:
-------------------------------------------------------------------------------
Test set: testea.server.MagicBoxTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.005 sec <<< FAILURE!
testDaNumber(testea.server.MagicBoxTest) Time elapsed: 0 sec <<< ERROR!
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
        at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at testea.server.MagicBoxTest.lookupBean(MagicBoxTest.java:37)
        at testea.server.MagicBoxTest.testDaNumber(MagicBoxTest.java:42)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
        at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)


What am I missing here? What should I change in order to get the expected behavior?

Thanks

Rudolf
[Message sent by forum member 'rudolfvlk']

http://forums.java.net/jive/thread.jspa?messageID=485483