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

时间:2010-11-19 23:04:35

标签: unit-testing glassfish maven

我在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 Embedded 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 Embedded API对Maven项目进行单元测试的经验?

2 个答案:

答案 0 :(得分:1)

查看JBoss的Arquillian项目。

  

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

具体instructions for Glassfish。我使用它来运行与JUnit(和maven)的集成测试。效果很好。管理嵌入式容器的细节被抽象掉了,除了他们在文档中引导您进行的一些初始配置。

答案 1 :(得分:0)

你可以试试这个适用于我的代码: 我根据我的glassfish标准安装floder来调整propreties

  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
相关问题