AEM 6.3, wcm.io: service test not working using OSGi annotations | Adobe Higher Education
Skip to main content
Level 4
February 7, 2018

AEM 6.3, wcm.io: service test not working using OSGi annotations

I am experiencing a test issue. My service tests work for my older services (Felix annotation based -> org.apache.felix.scr.annotations.Component) but when I try this for a newer service (OSGI annotation based -> org.osgi.service.component.annotations.Component), I get an error.

Service:

...
import org.osgi.service.component.annotations.Component;

@Component(service = TestService.class, immediate = true, property = {
  Constants.SERVICE_DESCRIPTION + "=" + "Test Service",
  Constants.SERVICE_VENDOR + "=" + BundleConstants.SERVICE_VENDOR
})
public class TestServiceImpl implements TestService {

@Override
public String getValue() {
  return "test-value-from-service";
  }
}

Sling Model:

@OSGiService
private TestService testService;

public String getValue(){
  return testService.getValue();
}

Register:

TestService testService = aemContext.registerInjectActivateService(new TestServiceImpl());

Error:

Caused by: org.apache.sling.testing.mock.osgi.NoScrMetadataException: No OSGi SCR metadata found for class com.asadventure.core.service.TestServiceImpl

enter image description here

Am I missing something here?

Ce sujet a été fermé aux réponses.

2 commentaires

smacdonald2008
Level 10
February 7, 2018

Appears to be many issues here. First - to use the new DS Annotations - read this:

Official OSGi Declarative Services Annotations in AEM - Adobe Experience Manager | AEM/CQ | Apache Sling

For Sling Models  - you should use this annotation:

@Model(adaptables = Resource.class)

public class Multifield

NOT:

Sling Model:

@OSGiService 

private TestService testService; 

  public String getValue(){ 

  return testService.getValue(); 

See this artilce to build an AEM Component that uses Sling Models --

Adobe Experience Manager Help | Creating a HTL Repeating Data Set 6.3 Component that uses Sling Models

that will show you how to build a Repeating Data Set HTL component that successfully uses a Sling Model and renders the data in the component .

Hope this helps...

Level 4
February 7, 2018

But my TestService is no sling model. It is a simple service, I don't want to inject anything like they do in Multifield example.

Level 4
February 7, 2018

I knew i documented a little example of a custom service in a Maven 11 Archetype. See this blog item titled "How can I test my AEM Code? "

here - Scott's Digital Community: Adobe Experience Manager FAQs and other Tips

We created a sample FOO service and then show how how to reference too from a SLING MODel and test it all.


Thanks, I'll take a look at those resources. By the way, by 'Sling Model' I meant that that code was part of a class annotated with

@Model(adaptables = Resource.class)

joerghoh
Adobe Employee
Adobe Employee
February 7, 2018

Hi,

the exception is descriptive:

Caused by: org.apache.sling.testing.mock.osgi.NoScrMetadataException: No OSGi SCR metadata found for class com.asadventure.core.service.TestServiceImpl

And as you stated, you moved away from the SCR annotations to the offical OSGI annotations. That means, that it's quite unlikely that OSGI annotations provide SCR metadata :-) And therefor the mocking library cannot find them anymore.

First recommendation: Update your mocking libraries and use the latest one. Maybe the problem is solved there.

If that does not work, please report your problem on the Sling mailing list.

Jörg

Level 4
February 7, 2018

Seems like I am using the latest versions, I am not sure if wcm io's AemContext can handle a OSGI R6 annotated service.

joerghoh
Adobe Employee
Adobe Employee
February 7, 2018

In that case Stefan Seifert (or more general: the wcm.io people) are probably interested in knowing that (they probably already do, but it's always nice to know that others are using your software :-)).

Jörg