在 Maven 项目的单元测试中使用 Glassfish Embedded API

发布于 2024-10-03 19:23:55 字数 3149 浏览 1 评论 0原文

我有一个在 Glassfish 3.0.1 上运行的 Maven 项目,在 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>
 <groupId>javax</groupId>
 <artifactId>javaee-web-api</artifactId>
 <version>6.0</version>
 <scope>provided</scope>
</dependency>
...

我一直在尝试使用 Glassfish 嵌入式 API 运行单元测试,如第一个依赖项中所述,但每次都会出现错误我尝试创建de EJBContainer。

测试类:

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

错误日志:

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)

有人有使用 Glassfish 嵌入式 API 进行单元测试 Maven 项目的经验吗?

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>
 <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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁客 2024-10-10 19:23:55

查看 JBoss 的 Arquillian 项目。

Arquillian 项目的使命
是提供一个简单的测试工具
抽象出所有容器
测试的生命周期和部署
逻辑,以便开发人员可以轻松生成
广泛的集成测试
他们的企业 Java 应用程序。

具体 Glassfish 说明。我用它来运行 JUnit(和 Maven)的集成测试。效果很好。除了文档中引导您完成的一些初始配置之外,管理嵌入式容器的详细信息已被抽象化。

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.

林空鹿饮溪 2024-10-10 19:23:55

你可以尝试这个代码,它对我有用:
我根据我的 glassfish 标准安装 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文