2012-03-27 62 views
1

我在將測試源中的Producer注入EntityManager到主源中的服務時遇到問題。目的是將該庫嵌入到另一個項目中,在該項目中,項目將使用它自己的PersistenceContext unitName定義Producer。如果我在主要來源中沒有生產者,我會得到Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName MY_PU錯誤。使用maven和嵌入式玻璃魚的CDI類路徑問題

例如:

/** src/main/java/org/sample/service */ 
@Stateless 
public class MyBean { 
    @Inject 
    @MyCustomQualifier 
    private EntityManager em; 
} 


/** src/test/java/org/sample/service */ 
public class EntityManagerProducer { 
    @Produces 
    @MyCustomQualifier 
    @PersistenceContext(unitName = "MY_PU") 
    private EntityManager em; 
} 

爲了讓我來測試服務,我必須使用maven螞蟻插件的* .class文件移動到target/classes目錄和包裝階段之前將其刪除。這工作,但它是kludgy。我試過在surefire插件中更改<useSystemClassLoader><useManifestOnlyJar>參數,但沒有成功。

還有別的辦法嗎?

我的環境

  • 的Maven:2.2.1
  • 神火:2.4.2
  • GlassFish的嵌入式全:3.1.1

編輯

beans.xml存在於(test/main)/ resources/META-INF

我開始GF容器如下。

Map<String, Object> properties = new HashMap<String, Object>(); 
properties.put(EJBContainer.MODULES, new File[]{new File("target/classes"), new File("target/test-classes")}); 
EJBContainer ejbContainer = EJBContainer.createEJBContainer(properties); 
Context ctx = ejbContainer.getContext(); 
MyBean service = ctx.lookup("java:global/ejb-app/classes/MyBean"); 
+0

聽起來像項目中的庫和類與glassfish加載的內容相沖突。我從來沒有對嵌入式容器和maven好運,因爲maven不能保持classpath的清潔。如果你在一個新的vm中啓動glassfish會發生什麼? – LightGuard 2012-03-27 16:52:25

+0

@LightGuard:默認情況下,surefire forkMode是'once'。所以嵌入式容器應該與maven分開的JVM。 (參考)(http://stackoverflow.com/questions/6813373/how-to-tell-maven2-to-execute-junit-tests-one-by-one-each-in-new-jvm-instance) – Hoon 2012-03-27 18:31:35

回答

0

好吧,我不看這個問題在所有的^^

你有 的src /測試/資源/ META-INF/beans.xml文件?

你的測試是怎樣的? Arquillian,還是你手動啓動容器? 您是否在類路徑中使用了MY_PU PersistenceUnit的persistence.xml?任何GitHub或那麼我可以看看示例項目?

我需要更多的信息關於您的環境,以幫助您...

+0

我我沒有使用Arquillian。簡單的'EJBContainer.createEJBContainer()'(注意上面的編輯)。是的,persistence.xml存在於類路徑中。對不起,沒有github。 – Hoon 2012-03-29 20:37:45

+0

特洛伊,你可以試試 Persistence.createEntityManagerFactory(「MY_PU」)。createEntityManager()? 如果這項工作,那麼我們可以嘗試進一步的步驟。 如果這已經給你一個無效的EM或根本沒有,那麼你的JPA安裝或persistence.xml有問題。 – struberg 2012-04-13 16:34:34

0

我們有一個類似的問題,降級到Maven 3.0.5。

看來這個問題在this Glassfish JIRA中討論過。我似乎無法找到討論中提到的Maven JIRA。我仍然在尋找一種方法來使用嵌入式Glassfish的最新Maven,但沒有太多運氣。