NetBeans 7.0,JUnit和Glassfish Embedded 3.1:不起作用

时间:2011-06-25 20:41:34

标签: java netbeans java-ee glassfish glassfish-embedded

我正在尝试使用NB7,JUnit和Win7Professional机器中的嵌入式Glassfish服务器JUnit测试J2EE Web应用程序。

我将其分解为仅使用单个会话Bean测试一个简单的Hello-World Web App。

我的classpath中有glasfish-embedded-static-shell.jar,并且还试图使用glassfish-embedded-all-3.1但它也没有用。

现在这里是我用来测试bean的代码:

import javax.ejb.embeddable.EJBContainer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class SimpleBeanTest {

private static EJBContainer container;

public SimpleBeanTest() {
}

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

@AfterClass
public static void tearDownClass() throws Exception {
    container.close();
}

@Test
public void testCountToThree() throws Exception {
   System.out.println("countToThree");
   SimpleBean instance = (SimpleBean) container.getContext().lookup("java:global/classes/SimpleBean");
   int expResult = 0;
   int result = instance.countToThree();
   assertEquals(expResult, result);

}
}

以下是它的结果:

19.06.2011 09:31:56 com.sun.enterprise.v3.server.CommonClassLoaderServiceImpl findDerbyClient
INFO: Cannot find javadb client jar file, derby jdbc driver will not be available by default.
19.06.2011 09:31:56 org.glassfish.flashlight.impl.provider.FlashlightProbeProviderFactory processXMLProbeProviders
SCHWERWIEGEND: MNTG0301:Cannot process XML ProbeProvider, xml = META-INF/gfprobe-provider.xml
java.lang.IllegalStateException: Provider already mapped glassfish:javamail:smtp-transport

(StackTrace here)

19.06.2011 09:31:57 org.glassfish.flashlight.impl.provider.FlashlightProbeProviderFactory processXMLProbeProviders
SCHWERWIEGEND: MNTG0301:Cannot process XML ProbeProvider, xml = META-INF/mojarra-jsf-api-probe-provider.xml
java.lang.IllegalStateException: Provider already mapped glassfish:jsf:faces-servlet

(StackTrace here)

19.06.2011 09:31:57 org.glassfish.flashlight.impl.provider.FlashlightProbeProviderFactory processXMLProbeProviders
SCHWERWIEGEND: MNTG0301:Cannot process XML ProbeProvider, xml = jersey-gf-server-probe-provider.xml
java.lang.IllegalStateException: Provider already mapped glassfish:jersey:server-hidden

(StackTrace here)

19.06.2011 09:31:57 org.glassfish.flashlight.impl.provider.FlashlightProbeProviderFactory processXMLProbeProviders
SCHWERWIEGEND: MNTG0301:Cannot process XML ProbeProvider, xml = jersey-gf-statsprovider-probe-provider.xml
java.lang.IllegalStateException: Provider already mapped glassfish:jersey:server

(StackTrace here)

19.06.2011 09:31:57 org.glassfish.ha.store.spi.BackingStoreFactoryRegistry register
INFO: Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry
19.06.2011 09:31:57 org.glassfish.ha.store.spi.BackingStoreFactoryRegistry register
INFO: Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry
19.06.2011 09:31:57 com.sun.enterprise.v3.server.AppServerStartup run
SCHWERWIEGEND: Startdienst konnte nicht gestartet werden: com.sun.enterprise.naming.GlassFishNamingBuilder
19.06.2011 09:31:57 com.sun.enterprise.v3.server.AppServerStartup run
INFO: GlassFish Server Open Source Edition 3.1 (43) Startzeit: Embedded (597ms), Startdienste(929ms), gesamt(1.526ms)
19.06.2011 09:31:57 org.glassfish.admin.mbeanserver.JMXStartupService$JMXConnectorsStarterThread run
INFO: JMXStartupService: JMXConnector system is disabled, skipping.

更新 这是任何测试都会抛出的异常:

No EJBContainer provider available
Provider named org.glassfish.ejb.embedded.EJBContainerProviderImpl threw unexpected exception at create EJBContainer: 
java.lang.RuntimeException
java.lang.RuntimeException: java.lang.IllegalStateException: AMX Booter MBean is already registered: amx-support:type=boot-amx
    at org.glassfish.internal.embedded.Server.<init>(Server.java:290)
    at org.glassfish.internal.embedded.Server.<init>(Server.java:66)
    at org.glassfish.internal.embedded.Server$Builder.build(Server.java:176)

(Stack Trace here)

Caused by: java.lang.IllegalStateException: AMX Booter MBean is already registered: amx-support:type=boot-amx

我已经重新安装了两次NetBeans,它也无效。

2 个答案:

答案 0 :(得分:3)

我有同样的问题,我做了以下事项:

1)我安装了Glassfish 3.1.1并将其链接到我的项目。首先我添加了服务器 - 工具 - 服务器 - 添加服务器...然后我右键单击我的EJB项目并选择属性 - 运行 - Glassfish 3.1.1
2)在我的EJB pom.xml中,我使用了这个依赖项(确保systemPath变量指向正确的文件):

<dependency>
    <groupId>org.glassfish.extras</groupId>
    <artifactId>glassfish-embedded-static-shell</artifactId>
    <version>3.1.1</version>
    <scope>system</scope>
    <systemPath>${glassfish.embedded-static-shell.jar}</systemPath>
</dependency>

3)在我的EJB pom.xml中,我删除了其他人说我必须添加它的依赖关系如果我收到错误“提供商已映射...”

<dependency>
    <groupId>org.glassfish.extras</groupId>
    <artifactId>glassfish-embedded-all</artifactId>
    <version>3.1.1</version>
    <scope>test</scope>
</dependency>

就是这样!现在我的EJB JUnit测试与JPA 2一样工作就像一个魅力!希望这有帮助!

答案 1 :(得分:1)

好像服务器正在启动正常(预计会有一些例外情况,但我无法确认它们是否全部存在)。

  • 您是否使用“运行&gt;测试项目”运行测试用例?
  • JUnit GUI是否会弹出?
  • 是否安装了JUnit? Netbeans 7.0现在询问您是否因为许可问题而下载JUnit ......
  • 您可以运行不需要嵌入式容器的单元测试吗?

尝试遵循这些步骤:http://javadude.wordpress.com/2010/02/22/tutorial-most-simple-test-application-for-embedded-glassfish-netbeans-hudson/(忘记Hudson部分)。 IDE应该为您包含依赖项。

哦,另外一件事,既然你正在使用Windows,那么尝试以管理员身份运行Netbeans(右键单击选项)并看看会发生什么并不会有什么坏处。

干杯,

<强>更新

也许你正在点击this bug:

查看启动容器是否适用于您:

@BeforeClass
public static void setUpClass() throws Exception {
    Map<String, Object> p = new HashMap<String, Object>();       
    p.put(EJBContainer.APP_NAME, "foo");
    container = EJBContainer.createEJBContainer(p);
}