Highlighted

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

Avatar

Avatar

Jdruwe

Avatar

Jdruwe

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

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
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

Avatar

Avatar

Jdruwe

Avatar

Jdruwe

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

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
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

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
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

Avatar

Avatar

Jdruwe

Avatar

Jdruwe

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

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

07-02-2018

If you want to build more examples that use Sling Models - we have plenty of community based resources. The examples build example components as opposed to a few lines of code snippets that do not perform any use case.

For example - if you are interested in Sling Models - see these Resources:

Creating a custom Touch UI Grid Component for Adobe Experience Manager

Creating a Granite/Coral 6.3 Multifield HTL component for Adobe Experience Manager

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

Adobe Experience Manager Help | Creating Adobe Experience Manager 6.3 Sling Model Components (this uses JSP as opposed to HTL Like the others)

Highlighted

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K
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

Avatar

Avatar

Jdruwe

Avatar

Jdruwe

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

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K
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