Mock Osgi Services via Sling Mocks

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Mock Osgi Services via Sling Mocks

Dominik Süß
Hi everyone,
in my case I would like to register a mocked version of an osgi service -
in the past IIRC I could have scr metadata in a mock class under
src/test/java and just register this one.

With the more recent osgi annotations to generate the scr metadata there is
an extra execution for scr generation as described in
http://felix.apache.org/documentation/faqs/apache-felix-bundle-plugin-faq.html#use-scr-metadata-generated-by-bnd-in-unit-tests

The problem: this will ignore classes in scr-metadata so I currently see no
way on how to mock an osgi service for a test case without creating an own
bundle with these mocks (along with the generated SCR metadata).

Is there any way to programmatically inject a service and inject the
corresponding metadata?

Cheers
Dominik
Reply | Threaded
Open this post in threaded view
|

RE: Mock Osgi Services via Sling Mocks

Stefan Seifert
hello dominik.

do you have a small example project to show the problem?
from what you describe this should be no problem with osgi mocks/sling mocks - but i do not get the exact problem you encounter.

if you want to mock an OSGi services it's easiest to use somehing like mockito and register it in the mock context with the "registerService" method.

stefan

>-----Original Message-----
>From: Dominik Süß [mailto:[hidden email]]
>Sent: Monday, September 3, 2018 1:49 PM
>To: [hidden email]
>Subject: Mock Osgi Services via Sling Mocks
>
>Hi everyone,
>in my case I would like to register a mocked version of an osgi service -
>in the past IIRC I could have scr metadata in a mock class under
>src/test/java and just register this one.
>
>With the more recent osgi annotations to generate the scr metadata there is
>an extra execution for scr generation as described in
>http://felix.apache.org/documentation/faqs/apache-felix-bundle-plugin-
>faq.html#use-scr-metadata-generated-by-bnd-in-unit-tests
>
>The problem: this will ignore classes in scr-metadata so I currently see no
>way on how to mock an osgi service for a test case without creating an own
>bundle with these mocks (along with the generated SCR metadata).
>
>Is there any way to programmatically inject a service and inject the
>corresponding metadata?
>
>Cheers
>Dominik
Reply | Threaded
Open this post in threaded view
|

Re: Mock Osgi Services via Sling Mocks

Dominik Süß
Hi Stefan,

you're right I just thought too complicated trying to register via SCR.

The snippet below seems to work just fine:

 @Spy
 PackageRegistry registry = new FSPackageRegistry();
[...]
BundleContext bundleContext = MockOsgi.newBundleContext();
bundleContext.registerService(registry.getClass().getName(), registry,
null);

Cheers
Dominik

On Mon, Sep 3, 2018 at 2:45 PM Stefan Seifert <[hidden email]>
wrote:

> hello dominik.
>
> do you have a small example project to show the problem?
> from what you describe this should be no problem with osgi mocks/sling
> mocks - but i do not get the exact problem you encounter.
>
> if you want to mock an OSGi services it's easiest to use somehing like
> mockito and register it in the mock context with the "registerService"
> method.
>
> stefan
>
> >-----Original Message-----
> >From: Dominik Süß [mailto:[hidden email]]
> >Sent: Monday, September 3, 2018 1:49 PM
> >To: [hidden email]
> >Subject: Mock Osgi Services via Sling Mocks
> >
> >Hi everyone,
> >in my case I would like to register a mocked version of an osgi service -
> >in the past IIRC I could have scr metadata in a mock class under
> >src/test/java and just register this one.
> >
> >With the more recent osgi annotations to generate the scr metadata there
> is
> >an extra execution for scr generation as described in
> >http://felix.apache.org/documentation/faqs/apache-felix-bundle-plugin-
> >faq.html#use-scr-metadata-generated-by-bnd-in-unit-tests
> >
> >The problem: this will ignore classes in scr-metadata so I currently see
> no
> >way on how to mock an osgi service for a test case without creating an own
> >bundle with these mocks (along with the generated SCR metadata).
> >
> >Is there any way to programmatically inject a service and inject the
> >corresponding metadata?
> >
> >Cheers
> >Dominik
>