简体   繁体   English

如何从其他模块注入类的实现-CDI

[英]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. 我有一个基于CDI的项目,该项目包含两个模块。 The core module has some classes which needs to be injected in another modules of the Web project. 核心模块具有一些类,需要将这些类注入Web项目的其他模块中。

Main concern is code fine and able to inject. 主要关注的是代码是否正确以及是否可以注入。 But it fails for the JUNIT test cases. 但是对于JUNIT测试用例却失败了。 One more thing to note here, if i run the parent project of both the modules, JUNIT is running as expected. 这里还要注意一件事,如果我同时运行两个模块的父项目,则JUNIT将按预期运行。 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: 看到openejb,我想您正在与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. 对我来说,使用tomee构建UnitTests的最有前途的方法是使用ApplicationComposer本身。 Examples see: smaller test and bigger test with extra module 示例请参见: 较小的测试带有附加模块的较大的测试

Using ApplicationComposer you can build up your Testcontainer at will. 使用ApplicationComposer,您可以随意构建Testcontainer。

Another question, do you really need to work with OpenEjb or do you only want to use it for embedded tests. 另一个问题,您真的需要使用OpenEjb还是只想将其用于嵌入式测试。 If the choice also could be Weld, perhaps take a look at cdiunit. 如果选择也可以是Weld,也许看看cdiunit。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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