[英]Explicite Local EJB not injected with Arquillian
我使用 Arquillian 來測試具有顯式本地和遠程接口的無狀態 session bean。 但在測試中,Arquillian 不會在具有本地接口類型的字段中“注入”任何內容,但它適用於遠程接口。
@Stateless
public class TestServiceImpl implements TestServiceLocal, TestServiceRemote {
public String greet() {
return "hallo";
}
}
遠程接口:
@Remote
public interface TestServiceRemote {
public String greet();
}
語言環境界面:
@Local
public interface TestServiceLocal {
public String greet();
}
這是測試:
@RunWith(Arquillian.class)
public class GenericEntityDaoEjbIntegrationTest {
@Deployment
public static JavaArchive createTestArchive()
throws UnsupportedEncodingException {
return ShrinkWrap.create(JavaArchive.class, "test.jar")
.addClasses(
TestServiceLocal.class,
TestServiceRemote.class,
TestServiceImpl.class);
}
@EJB
private TestServiceLocal testServiceLocal;
@EJB
private TestServiceRemote testServiceRemote;
//Will Fail
@Test
public void testTestServiceLocal() {
assertNotNull(this.testServiceLocal);
}
//Success
@Test
public void testTestServiceRemote() {
assertNotNull(this.testServiceRemote);
}
}
我正在使用 arquillian-glassfish-embedded 1.0.0.CR2、glassfish-embedded-all 3.1 和 arquillian-junit-container 1.0.0.CR5 我的 pom 的相關部分是:
<!-- arquillian test -->
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.0.0.CR5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-spi</artifactId>
<version>1.0.0.CR5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.0.CR2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
這是日志文件的相關部分(不包含任何異常):
10.04.2012 15:38:16 com.sun.ejb.containers.BaseContainer initializeHome
INFO: Portable JNDI names for EJB TestServiceImpl : [java:global/test/TestServiceImpl!de.test.service.TestServiceRemote, java:global/test/TestServiceImpl!de.test.service.TestServiceLocal]
10.04.2012 15:38:16 com.sun.ejb.containers.BaseContainer initializeHome
INFO: Glassfish-specific (Non-portable) JNDI names for EJB TestServiceImpl : [de.test.service.TestServiceRemote, de.test.service.TestServiceRemote#de.test.service.TestServiceRemote]
10.04.2012 15:38:16 com.sun.enterprise.web.WebApplication start
INFO: WEB0671: Loading application [test] at [/test]
10.04.2012 15:38:16 org.glassfish.deployment.admin.DeployCommand execute
INFO: test was successfully deployed in 11.844 milliseconds.
我的錯誤是什么? 我還需要更改為語言環境接口注入的實例嗎?
您可以使用以下其中一項:
將beans.xml
文件添加到部署中:
return ShrinkWrap.create(JavaArchive.class, "test.jar").addClasses( TestServiceLocal.class, TestServiceRemote.class, TestServiceImpl.class).addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
這啟用了 Arquillian 的 CDITestEnricher,它比 EJBTestEnricher 更強大。 它可以處理@Inject
注釋(顯然),還可以處理@Resource
和@EJB
注釋(請參閱CDI 規范中有關資源注入的部分)。 然后容器將測試 class 實例中的兩個@EJB
注釋字段視為注入點並注入依賴項。
為具有已部署 bean 的可移植 JNDI 名稱的字段指定@EJB
注釋的mappedName
屬性。 在您的情況下,它看起來像:
@EJB(mappedName="java:global/test/TestServiceImpl.com.acme;TestServiceLocal") private TestServiceLocal testServiceLocal;
您需要確保可移植 JNDI 名稱與為您的部署生成的名稱相同。 我只是指定了為我的“com.acme.TestServiceLocal”接口生成的那個。
除了答案之外,您還必須確保您使用正確的@Deployment
設置是正確的。 要在本地注入,您必須確保擁有@Deployment(testable=true)
(請注意,這是默認設置)。
來自Aquillian 文檔:
容器內模式:@Deployment(testable = true)
正如我們上面提到的,我們需要重新打包您的@Deployment,添加一些 Arquillian 支持類,以便在容器中運行。 這使我們能夠與測試進行通信,豐富測試並遠程運行測試。 在這種模式下,測試在遠程容器中執行; Arquillian 默認使用這種模式。
當您提供 @Deployment 時,請參閱完整協議參考以了解打包過程的預期 output 的概述。
客戶端模式:@Deployment(testable = false)
現在這個模式是簡單的部分。 與重新打包和覆蓋測試執行的容器內模式相反,客戶端模式盡可能少。 它不會重新打包您的@Deployment,也不會將測試執行轉發到遠程服務器。 您的測試用例按預期在您的 JVM 中運行,您可以自由地從外部測試容器,就像您的客戶看到的那樣。 Arquillian 唯一做的就是控制@Deployment 的生命周期。
下面是一個使用 as client 模式調用 Servlet 的例子。
這對 OP 沒有幫助(他們有正確的設置),但我希望它能像我一樣幫助那些來自谷歌的人。
嘗試更改 TestServiceImpl->TestServiceBean 看起來嵌入式 glassfish 對 bean 名稱有特定要求
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.