@reference in Unit Tests
Hello, hopefully the last question this week.
I inject my Service in the servlet with:
@Reference
IRssFeed rssFeed;
The question is now how to do it with AEM Context.
@Test
public void testDoGetForJson() throws IOException, WCMException {
context.registerService(IRssFeed.class, new RssFeedImpl());
}
I also tried the other ways, but the rssFeed is always null.
// register OSGi service
context.registerService(MyClass.class, myService);
// or alternatively: inject dependencies, activate and register OSGi service
context.registerInjectActivateService(myService);
At first step I added the maven-build plugin to my pom.xml but afterwards i get some errors for old testcases:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.3.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>scr-metadata</id>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<exportScr>true</exportScr>
<instructions>
<Embed-Dependency>
mailjet-client,
http-client-java,
solr-solrj,
zookeeper,
gson,
rome,
rome-utils,
jdom2
</Embed-Dependency>
<Export-Package>
com.test.aem.relaunch.*,net.tanesha.recaptcha,net.tanesha.recaptcha.http
</Export-Package>
<Import-Package>
javax.inject;version=0.0.0,
*;resolution:=optional
</Import-Package>
<Sling-Model-Packages>com.test.aem.relaunch.*</Sling-Model-Packages>
<_dsannotations>*</_dsannotations>
<!-- Enable processing of OSGI metatype annotations -->
<_metatypeannotations>*</_metatypeannotations>
<!--<_plugin>org.apache.felix.scrplugin.bnd.SCRDescriptorBndPlugin;destdir=${project.build.outputDirectory};</_plugin>-->
</instructions>
</configuration>
</plugin>
This is one example of the errors appeared after adding the plugin:
java.lang.RuntimeException: Unable to read classpath resource: OSGI-INF/metatype/com.test.aem.relaunch.schedulers.JobSearchScheduler.xml
at org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.toXmlDocument(OsgiMetadataUtil.java:230)
at org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.parseMetadataDocument(OsgiMetadataUtil.java:194)
at org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.parseMetadataDocuments(OsgiMetadataUtil.java:183)
at org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.initMetadataDocumentCache(OsgiMetadataUtil.java:171)
at org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.<clinit>(OsgiMetadataUtil.java:114)
at org.apache.sling.testing.mock.osgi.MapMergeUtil.propertiesMergeWithOsgiMetadata(MapMergeUtil.java:73)
at org.apache.sling.testing.mock.osgi.MapMergeUtil.propertiesMergeWithOsgiMetadata(MapMergeUtil.java:55)
at org.apache.sling.testing.mock.osgi.MockBundleContext.registerService(MockBundleContext.java:120)
at org.apache.sling.testing.mock.osgi.MockBundleContext.registerService(MockBundleContext.java:108)
at org.apache.sling.testing.mock.osgi.MockBundleContext.<init>(MockBundleContext.java:81)
at org.apache.sling.testing.mock.osgi.MockOsgi.newBundleContext(MockOsgi.java:50)
at org.apache.sling.testing.mock.osgi.ComponentContextBuilder.build(ComponentContextBuilder.java:69)
at org.apache.sling.testing.mock.osgi.MockOsgi.newComponentContext(MockOsgi.java:67)
at org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.componentContext(OsgiContextImpl.java:74)
at org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.bundleContext(OsgiContextImpl.java:83)
at org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.registerInjectActivateService(OsgiContextImpl.java:153)
at org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.registerInjectActivateService(OsgiContextImpl.java:141)
at org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.registerDefaultServices(OsgiContextImpl.java:66)
at org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.setUp(OsgiContextImpl.java:47)
at org.apache.sling.testing.mock.sling.context.SlingContextImpl.setUp(SlingContextImpl.java:103)
at io.wcm.testing.mock.aem.context.AemContextImpl.setUp(AemContextImpl.java:74)
at io.wcm.testing.mock.aem.junit.AemContext.access$100(AemContext.java:48)
at io.wcm.testing.mock.aem.junit.AemContext$1.before(AemContext.java:156)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.xml.sax.SAXParseException; Premature end of file.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
at org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.toXmlDocument(OsgiMetadataUtil.java:226)
... 40 more
if I use this:
IRssFeed rssFeed = new RssFeedImpl();
MockOsgi.injectServices(rssFeed, bundleContext);
MockOsgi.activate(rssFeed,bundleContext);
The error message change to:
org.apache.sling.testing.mock.osgi.NoScrMetadataException: No OSGi SCR metadata found for class com.test.aem.relaunch.services.rss.RssFeedImpl
I thought the maven plugin will create it for me?
So is there any other way to inject the reference? Or someone sees a mistake?