2011-06-25 56 views
6

我想要JUnit在Win7Professional機器上用NB 7.0,JUnit和嵌入式Glassfish服務器測試J2EE Web應用程序。NetBeans 7.0,JUnit和Glasfish嵌入式3.1:不起作用

我把它分解爲只用一個Session Bean測試一個簡單的Hello-World Web App。

我的classpath中有glasfish-embedded-static-shell.jar,並且試圖使用glassfish-embedded-all-3.1,但它也沒有工作。

現在,這裏是我用來測試豆代碼:

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. 

UPDATE: 這是這將是例外拋出任何測試:

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兩次,但它也不起作用。

回答

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我刪除依賴別人說我來補充它,如果我得到錯誤「Provider already mapped ...」

<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

似乎服務器開始正常(其中一些例外情況是預期的,但我不能確認它們是否都是)。

  • 您是否在運行測試用例「Run> Test Project」?
  • 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(右鍵單擊選項)並查看會發生什麼並不會有什麼影響。

乾杯,

更新

也許你打this錯誤:

看看象這樣啓動容器爲你工作:

@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); 
} 
+0

嗨安東尼,我試了相同的結果教程。 是的,當我嘗試運行GUI出現的測試時,安裝了JUNit,它與NB安裝一起提供。 – SDReyes

+0

@DGalvis。嗨,這是什麼問題,它掛了嗎?它甚至顯示「countToThree」? –

+0

我試圖安裝NB完整版沒有運氣。 使用管理權限也不起作用。 – SDReyes