@Inject fails in tested code but works in test class
robgartman Sep 30, 2010 5:44 PMI have troubbles getting CDI injection working when injection is happening in other classes than the actual test class. I try to inject a bean into a class being referenced by the test case but I end up with a null object every time. However, injection within the test class itself works just fine. I'm rather new to CDI and these failures makes me wonder if it's even supposed to work.
The code below is an example of what I'm trying to do. First the "MyInjectedClass" class:
@Dependent public class MyInjectClass { Logger logger = LoggerFactory.getLogger(this.getClass()); public MyInjectClass() { logger.info("MyInjectClass instantiated"); } }
I want to inject MyInjectedClass above in MyClass below.
@ApplicationScoped public class MyClass { Logger logger = LoggerFactory.getLogger(this.getClass()); @Inject MyInjectClass myInjectClass; public MyClass(){ logger.info("Myclass OK"); logger.info("myInjectClass={}",myInjectClass); } Object getInjectedObject(){ return myInjectClass; } }
And this is my test setup. Test1 succeeds and Test2 fails since the "@Inject MyInjectClass myInjectClass;" results in a null object.
@RunWith(Arquillian.class) @Run(RunModeType.IN_CONTAINER) public class TestSuite { @Deployment public static Archive<?> createDeploymentPackage() { return ShrinkWrap.create(JavaArchive.class, "test.jar").addPackages(true, MyClass.class.getPackage()) .addManifestResource(new ByteArrayAsset("<beans />".getBytes()), ArchivePaths.create("beans.xml")); } @Inject MyClass myClass; @Test public void Test1() throws Exception { Assert.assertNotNull("Injection did not work from within TestSuite",myClass); } @Test public void Test2() throws Exception { Assert.assertNotNull("Injection did not work from within MyClass",myClass.getInjectedObject()); } }
My environment is:
Apache Maven 3.0-beta-3 (r990787; 2010-08-30 14:44:03+0200) Java version: 1.6.0_21 OS name: "windows 7" version: "6.1" arch: "amd64" Family: "windows" Tests are running on Glassfish embedded v.3.1-b20 and Arquillian 1.0.0.Alpha4.SP1
The Maven Surefire output:
2010-sep-30 22:17:47 com.sun.enterprise.glassfish.bootstrap.EmbeddedNonOSGiGlassFishRuntimeBuilder provisionInstanceRoot VARNING: AS_DEF_DOMAINS_PATH is not set. 2010-sep-30 22:17:48 com.sun.enterprise.util.EarlyLogger add INFO: Total time to parse domain.xml: 79 milliseconds 2010-sep-30 22:17:50 com.sun.logging.LogDomains$1 log INFO: GlassFish Server Open Source Edition 3.1-b20 (java_re-private) startup time : Embedded(454ms) startup services(2482ms) total(2936ms) 2010-sep-30 22:17:50 com.sun.logging.LogDomains$1 log INFO: enterprise_used_delegate_name 2010-sep-30 22:17:50 com.sun.logging.LogDomains$1 log INFO: JMXStartupService: JMXConnector system is disabled, skipping. 2010-sep-30 22:17:50 AppServerStartup run INFO: [Thread[GlassFish Kernel Main Thread,5,main]] started 2010-sep-30 22:17:50 org.glassfish.web.embed.impl.EmbeddedWebContainerImpl bind INFO: EmbeddedWebContainer binding port 8181 protocol http 2010-sep-30 22:17:50 org.hibernate.validator.util.Version <clinit> INFO: Hibernate Validator null 2010-sep-30 22:17:50 org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. 2010-sep-30 22:17:51 com.sun.enterprise.v3.server.DomainXmlPersistence save ALLVARLIG: domain.xml cannot be persisted, null destination 2010-sep-30 22:17:51 com.sun.enterprise.v3.server.DomainXmlPersistence save ALLVARLIG: domain.xml cannot be persisted, null destination 2010-sep-30 22:17:51 com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 onReady INFO: Grizzly Framework 1.9.19 started in: 72ms - bound to [0.0.0.0:8181] 2010-sep-30 22:17:51 com.sun.enterprise.v3.server.DomainXmlPersistence save ALLVARLIG: domain.xml cannot be persisted, null destination 2010-sep-30 22:17:52 com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 onReady INFO: Grizzly Framework 1.9.19 started in: 4ms - bound to [0.0.0.0:8181] 2010-sep-30 22:17:52 org.jboss.arquillian.impl.DynamicServiceLoader verifySameImplementation VARNING: More then one reference to the same implementation was found for org.jboss.arquillian.spi.DeploymentPackager, please verify you classpath 2010-sep-30 22:17:52 org.jboss.arquillian.impl.DynamicServiceLoader verifySameImplementation VARNING: More then one reference to the same implementation was found for org.jboss.arquillian.spi.ApplicationArchiveGenerator, please verify you classpath 2010-sep-30 22:17:52 org.jboss.shrinkwrap.impl.base.ServiceExtensionLoader findExtensionImpl VARNING: Multiple extension implementations found for org.jboss.shrinkwrap.api.spec.JavaArchive, please verify classpath or add a extensionOverride 2010-sep-30 22:17:52 org.jboss.shrinkwrap.impl.base.ServiceExtensionLoader findExtensionImpl VARNING: Multiple extension implementations found for org.jboss.shrinkwrap.api.spec.WebArchive, please verify classpath or add a extensionOverride 2010-sep-30 22:17:52 org.jboss.shrinkwrap.impl.base.ServiceExtensionLoader findExtensionImpl VARNING: Multiple extension implementations found for org.jboss.shrinkwrap.glassfish.api.ShrinkwrapReadableArchive, please verify classpath or add a extensionOverride 2010-sep-30 22:17:52 org.jboss.shrinkwrap.impl.base.ServiceExtensionLoader findExtensionImpl VARNING: Multiple extension implementations found for org.jboss.shrinkwrap.api.exporter.ZipExporter, please verify classpath or add a extensionOverride 2010-sep-30 22:17:52 org.jboss.shrinkwrap.impl.base.ServiceExtensionLoader findExtensionImpl VARNING: Multiple extension implementations found for org.jboss.shrinkwrap.spi.Configurable, please verify classpath or add a extensionOverride 2010-sep-30 22:17:53 com.sun.logging.LogDomains$1 log INFO: security.secmgroff 2010-sep-30 22:17:53 com.sun.logging.LogDomains$1 log INFO: sec.service.startup.enter 2010-sep-30 22:17:53 com.sun.logging.LogDomains$1 log INFO: policy.loading 2010-sep-30 22:17:53 com.sun.logging.LogDomains$1 log INFO: realm.loaded.successfully 2010-sep-30 22:17:53 com.sun.logging.LogDomains$1 log INFO: realm.loaded.successfully 2010-sep-30 22:17:53 com.sun.logging.LogDomains$1 log INFO: realm.loaded.successfully 2010-sep-30 22:17:53 com.sun.logging.LogDomains$1 log INFO: sec.service.startup.exit 2010-sep-30 22:17:53 com.sun.common.util.logging.LoggingConfigImpl openPropFile INFO: Cannot read logging.properties file. 2010-sep-30 22:17:53 com.sun.logging.LogDomains$1 log INFO: webContainer.HTTP.listenerAndPort 2010-sep-30 22:17:53 com.sun.logging.LogDomains$1 log INFO: webContainer.virtualServer.created 2010-sep-30 22:17:54 com.sun.logging.LogDomains$1 log INFO: PersistenceStrategyBuilderFactory>>createPersistenceStrategyBuilder: CandidateBuilderClassName = class com.sun.enterprise.web.MemoryStrategyBuilder 2010-sep-30 22:17:54 com.sun.logging.LogDomains$1 log INFO: webContainer.virtualServer.loadedDefaultWebModule classLoader = WebappClassLoader (delegate=true; repositories=WEB-INF/classes/) SharedSecrets.getJavaNetAccess()=java.net.URLClassLoader$7@6ea53502 2010-sep-30 22:18:03 org.jboss.weld.bootstrap.WeldBootstrap <clinit> INFO: WELD-000900 SNAPSHOT 2010-sep-30 22:18:03 org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. 2010-sep-30 22:18:04 com.sun.logging.LogDomains$1 log INFO: PersistenceStrategyBuilderFactory>>createPersistenceStrategyBuilder: CandidateBuilderClassName = class com.sun.enterprise.web.MemoryStrategyBuilder 2010-sep-30 22:18:04 com.sun.logging.LogDomains$1 log INFO: webApplication.loadingApplication 2010-sep-30 22:18:04 MyClass <init> INFO: Myclass OK 2010-sep-30 22:18:04 MyClass <init> INFO: myInjectClass=null
Are there any helpful minds in the community who can tell me what's wrong?
Best regards
Robert