2012-07-12 40 views
1

我在MyEclipse 10.0中使用了WAS 8.0,EJB 3.1,CDI和EJB可嵌入容器。還有beans.xml定義和可嵌入的容器jar在classpath中。在測試類中的EJB引用(課程)上獲取運行時空值。JUnit測試用例中@EJB注入的空值

公共類CourseTest延伸測試用例{

@EJB  
private Course course; 

@Before 
public void setUp() throws Exception { 
    EJBContainer.createEJBContainer().getContext().bind("inject", this); 
} 

@Test 
public void test() { 

    // Was the EJB injected? 
    assertTrue(course != null); 

回答

0

你的測試用例類不是由容器構成,所以注入將不會被執行。您需要手動查找來自EJBContainer.createEJBContainer().getContext()的EJB。

(順便說一句,該API的EJBContainer只允許每個進程一種活性EJB容器。我建議存儲EJBContainer.createEJBContainer()結果中的一個字段,從@After方法關閉它。)