Highlighted

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

Jdruwe

07-02-2018

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?

Replies

Highlighted

smacdonald2008

07-02-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 S...

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 Mode...

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...

Highlighted

Jdruwe

07-02-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.

Highlighted

smacdonald2008

07-02-2018

In your thread - it looks like you want to work with a Sling Model.

If all you want to do is create a service and get it running - i recommend going through this article - it uses Maven Archetype 11 and will use DS Annotations.

Take a look at some of the default Java files that use this annotation:

org.osgi.service.component.annotations.Component

Creating an Adobe Experience Manager 6.3 Project using Adobe Maven Archetype 11

This will get you familiar with DS Annotations and how to build AEM Projects that use them. ALso - read the article I linked above on DS ANNotations.

Highlighted

smacdonald2008

07-02-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.

Highlighted

Jdruwe

07-02-2018

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)

Highlighted
Highlighted

Jörg_Hoh

Employee

07-02-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

Highlighted

Jdruwe

07-02-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.

Highlighted

Jörg_Hoh

Employee

07-02-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