简体   繁体   中英

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

I'm having a project based on CDI which consists of two modules. The core module has some classes which needs to be injected in another modules of the Web project.

Main concern is code fine and able to inject. But it fails for the JUNIT test cases. One more thing to note here, if i run the parent project of both the modules, JUNIT is running as expected. And if i run module specific, throws errors like below.

Error logs:

       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

Will appreciate your help!

Seeing openejb, I suppose you are working with tomee:

My own primitive tests with this server brought me to the impression, that it does incredible things to make the tests work without doing any big coding. That seems to work until a certain point. But it is not very reliable.

For me the most promising way in building UnitTests with tomee was to use the ApplicationComposer itself. Examples see: smaller test and bigger test with extra module

Using ApplicationComposer you can build up your Testcontainer at will.

Another question, do you really need to work with OpenEjb or do you only want to use it for embedded tests. If the choice also could be Weld, perhaps take a look at cdiunit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM