开发者

Using Glassfish Embedded API in unit tests of Maven project

开发者 https://www.devze.com 2023-01-26 14:46 出处:网络
I have a Maven Project running on Glassfish 3.0.1 with these dependencies in the pom.xml: ... <dependency>

I have a Maven Project running on Glassfish 3.0.1 with these dependencies in the pom.xml:

...
<dependency>
 <groupId>org.glassfish.extras</groupId>
 <artifactId>glassfish-embedded-all</artifactId>
    <version>3.0.1</version>
    <scope>test</scope>
</dependency>
<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.8.2</version>
 <scope>test</scope>
</dependency>
<dependency&g开发者_开发技巧t;
 <groupId>javax</groupId>
 <artifactId>javaee-web-api</artifactId>
 <version>6.0</version>
 <scope>provided</scope>
</dependency>
...

I've been trying to run unit tests using the Glassfish Embedded API, as mentioned in the first dependency, but it gives me an error every time I try to create de EJBContainer.

test class:

...
@BeforeClass
public static void setUpClass() throws Exception {
    EJBContainer ejbC = javax.ejb.embeddable.EJBContainer.createContainer();
}
...

error log:

javax.ejb.EJBException: No EJBContainer provider available
The following providers:
org.glassfish.ejb.embedded.EJBContainerProviderImpl
Returned null from createEJBContainer call.

 at javax.ejb.embeddable.EJBContainer.reportError(EJBContainer.java:186)
 at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:121)
 at br.com.code.seuticket.sms.bean.GatewayBeanClickatellImplTest.setUpClass(GatewayBeanClickatellImplTest.java:53)
 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.RunBefores.evaluate(RunBefores.java:27)
 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)

Does anyone has experience with unit testing Maven projects with Glassfish Embedded API?


Check out the Arquillian project by JBoss.

The mission of the Arquillian project is to provide a simple test harness that abstracts away all container lifecycle and deployment from the test logic so developers can easily produce a broad range of integration tests for their enterprise Java applications.

Specific instructions for Glassfish. I use it for running my integration test with JUnit (and maven). Works great. The details of managing the embedded container are abstracted away, except for a little bit of initial configuration that they walk you through in the docs.


you can try this code it works for me : i adjust propreties according to my glassfish standard installation floder

  Map<String, Object> properties = new HashMap<>();
    properties.put(EJBContainer.MODULES, new File("target/classes/cd/espoirmur/ejb"));
    properties.put("installation.root", "C:\\Program Files\\glassfish-4.1");
    properties.put("instance.root", "C:\\Program Files\\glassfish-4.1\\glassfish\\domains\\domain1");
    properties.put("configuration.file", "C:\\Program Files\\glassfish-4.1\\glassfish\\domains\\domain1\\config\\domainEmbeded.xml");
    EJBContainer ec = EJBContainer.createEJBContainer(properties);
    System.out.println("--------------ejb container sucessfully created----------");
    Context ctx = ec.getContext();
    System.out.println("--------------ejb context successfull  sucessfully created----------");
enter code here
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号