我正在尝试在Win7Professional机器中使用NB 7.0、JUnit和嵌入式Glassfish Server对J2EE Web应用程序进行JUnit测试。
我把它分解为用一个会话Bean测试一个简单的Hello World Web应用程序。
我的类路径中有glasfish-embedded-static-shell.jar,也尝试过使用glassfish-embedded-all3.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,它也不起作用。
我遇到了同样的问题,我做了以下操作:
1) 我安装了Glassfish 3.1.1并将其链接到我的项目中。首先我用工具添加了服务器-服务器-添加服务器。。。然后,我右键单击EJB项目并选择Properties-Run-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>
就是这样!现在,我用JPA2进行的EJB JUnit测试工作起来很有魅力!希望这能有所帮助!
看起来服务器启动正常(其中一些异常是意料之中的,但我无法确认是否全部都是)。
- 您是否使用"运行>测试项目"运行测试用例
- JUnit GUI弹出了吗
- 是否安装了JUnit?Netbeans 7.0现在询问您是否因为许可问题而想要下载JUnit
- 你能运行不需要嵌入式容器的单元测试吗
请尝试遵循以下步骤:http://javadude.wordpress.com/2010/02/22/tutorial-most-simple-test-application-for-embedded-glassfish-netbeans-hudson/(忘记哈德逊的部分)。IDE应该负责为您包含依赖项。
哦,还有一件事,既然你使用的是Windows,尝试以管理员身份运行Netbeans(右键单击选项)并看看会发生什么也没什么坏处。
干杯,
更新
也许你遇到了这个错误:
看看像这样启动容器是否适合你:
@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);
}