簡體   English   中英

如何從其他模塊注入類的實現-CDI

[英]how to inject implementation of the class from the other module - CDI

我有一個基於CDI的項目,該項目包含兩個模塊。 核心模塊具有一些類,需要將這些類注入Web項目的其他模塊中。

主要關注的是代碼是否正確以及是否可以注入。 但是對於JUNIT測試用例卻失敗了。 這里還要注意一件事,如果我同時運行兩個模塊的父項目,則JUNIT將按預期運行。 如果我運行特定於模塊,則會引發如下錯誤。

錯誤日志:

       INFO - OpenWebBeans Container is starting...
INFO - Adding OpenWebBeansPlugin : [CdiPlugin]
SEVERE - CDI Beans module deployment failed
org.apache.webbeans.exception.inject.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Api type [com.walmart.gls.move.location.core.domain.CacheService] is not found with the qualifiers 
Qualifiers: [@javax.enterprise.inject.Default()]
for injection into Field Injection Point, field name :  cacheService, Bean Owner : [PurgeHistorysScheduler, Name:null, WebBeans Type:ENTERPRISE, API Types:[java.lang.Object,java.io.Serializable,com.walmart.move.location.infra.scheduler.PurgeHistorysScheduler], Qualifiers:[javax.enterprise.inject.Default,javax.enterprise.inject.Any]]
    at org.apache.webbeans.config.BeansDeployer.deploy(BeansDeployer.java:215)
    at org.apache.openejb.cdi.OpenEJBLifecycle.startApplication(OpenEJBLifecycle.java:194)
    at org.apache.openejb.cdi.ThreadSingletonServiceImpl.initialize(ThreadSingletonServiceImpl.java:160)
    at org.apache.openejb.cdi.CdiBuilder.build(CdiBuilder.java:41)
    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:797)
    at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:623)
    at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:335)
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:56)
    at com.walmart.move.location.infra.common.BaseTest.setup(BaseTest.java:69)
    at com.walmart.move.location.infra.scheduler.test.TestPurgeHistorysScheduler.setup(TestPurgeHistorysScheduler.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Api type [com.walmart.gls.move.location.core.domain.CacheService] is not found with the qualifiers 
Qualifiers: [@javax.enterprise.inject.Default()]
for injection into Field Injection Point, field name :  cacheService, Bean Owner : [PurgeHistorysScheduler, Name:null, WebBeans Type:ENTERPRISE, API Types:[java.lang.Object,java.io.Serializable,com.walmart.move.location.infra.scheduler.PurgeHistorysScheduler], Qualifiers:[javax.enterprise.inject.Default,javax.enterprise.inject.Any]]
    at org.apache.webbeans.util.InjectionExceptionUtil.throwUnsatisfiedResolutionException(InjectionExceptionUtil.java:60)
    at org.apache.webbeans.container.InjectionResolver.checkInjectionPoint(InjectionResolver.java:195)
    at org.apache.webbeans.container.BeanManagerImpl.validate(BeanManagerImpl.java:955)
    at org.apache.webbeans.config.BeansDeployer.validate(BeansDeployer.java:491)
    at org.apache.webbeans.config.BeansDeployer.validateInjectionPoints(BeansDeployer.java:422)
    at org.apache.webbeans.config.BeansDeployer.deploy(BeansDeployer.java:202)
    ... 31 more

將感謝您的幫助!

看到openejb,我想您正在與tomee合作:

我在該服務器上進行的原始測試給我留下了印象,即它無需進行大量編碼就可以使測試正常工作,從而完成了令人難以置信的工作。 這似乎可以工作到一定程度。 但這不是很可靠。

對我來說,使用tomee構建UnitTests的最有前途的方法是使用ApplicationComposer本身。 示例請參見: 較小的測試帶有附加模塊的較大的測試

使用ApplicationComposer,您可以隨意構建Testcontainer。

另一個問題,您真的需要使用OpenEjb還是只想將其用於嵌入式測試。 如果選擇也可以是Weld,也許看看cdiunit。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM