Expand my Community achievements bar.

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

Avatar

Level 5

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?

12 Replies

Avatar

Level 10

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

Avatar

Level 5

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.

Avatar

Level 10

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.

Avatar

Level 10

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.

Avatar

Level 5

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)

Avatar

Level 10

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)

Avatar

Employee Advisor

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

Avatar

Level 5

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

Avatar

Employee Advisor

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

Avatar

Level 5

The weird things is that I code as described here: Apache Sling :: OSGi Mocks I still get the 'No OSGi SCR metadata found for class com.asadventure.core.service.TestServiceImpl' error:

Test:

@Rule

public final OsgiContext context = new OsgiContext();

@Test

public void doTest() {

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

String value = testService.getValue();

assertEquals(value, "test-value-from-service");

}

Service implementation:

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

@Component(service = TestService.class, immediate = true)

public class TestServiceImpl implements TestService {

@Override

public String getValue() {

return "test-value-from-service";

}

}

Service interface:

public interface TestService {

public String getValue();

}

I am using version 2.3.6 of 'org.apache.sling.testing.osgi-mock'

Avatar

Level 10

Test the code with the JUNIT stuff as you see when you build a Maven 11 Archetype.

It could be that org.apache.sling.testing.osgi-mock only works with SRC annotations.

Avatar

Level 5

I found out that you had to specify some info in the maven-bundle-plugin:Apache Felix - Apache Felix Bundle Plugin Frequently Asked Questions --> Use SCR metadata generated by BND in Unit Tests. Now my OSGI annotated services test successfully but my felix ones give me the same old error: 'No OSGi SCR metadata found'