Issues getting service (Externalizer) in unit tests

596 views
Skip to first unread message

Alex Theys

unread,
Mar 2, 2016, 7:05:29 AM3/2/16
to wcm-io Developers
Hi,

I am having trouble with getting an AEM specific service from inside a unit test class.
I have tried using the context.getService() method as well as using the service reference with the bundle context.

I'd like to get Externalizer and inject it into the xlass I am testing, however it always resolves as null:
Code:
Externalizer service = context.getService(Externalizer.class);


Or code:
ServiceReference ref = context.bundleContext().getServiceReference(Externalizer.class.getName());
Externalizer service2 = (Externalizer) context.bundleContext().getService(ref);

Am I doing something wrong or misunderstanding the steps to retrieve an OSGI service?

Cheers,
Alex

Alex Theys

unread,
Mar 2, 2016, 7:08:21 AM3/2/16
to wcm-io Developers
I should add that I'll have to use the same process for HtmlLibraryManager

Stefan Seifert

unread,
Mar 2, 2016, 7:37:09 AM3/2/16
to wcm-i...@googlegroups.com
hello alex.

please note that AEM mock implements only a tiny subset of the AEM API out of the box (but the most important parts).
the features that are supported are described here: http://wcm.io/testing/aem-mock/

in your case (code using Externalizer or HtmlLibraryManager) it is the easiest solution to create a dynamic mock yourself e.g. using Mockito, and register it to the OSGi context in the unit test:

context.registerService(Externalizer.class, mockExternalizer);

then you can define the expected behavior of the mocked externalizer and validate calls to its methods.

stefan
>--
>You received this message because you are subscribed to the Google Groups
>"wcm-io Developers" group.
>To unsubscribe from this group and stop receiving emails from it, send an
>email to wcm-io-dev+...@googlegroups.com.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/wcm-io-dev/e313db9a-5a23-4caf-9636-
>12da64ba818f%40googlegroups.com.
>For more options, visit https://groups.google.com/d/optout.

Alex Theys

unread,
Mar 2, 2016, 7:40:29 AM3/2/16
to wcm-io Developers
Hi Stefan,

I see, I thought it might be the case, I wanted to be sure.

Cheers,
Alex

Alex Theys

unread,
Mar 2, 2016, 7:40:52 AM3/2/16
to wcm-io Developers
Thanks for the quick reply!

Stefan Seifert

unread,
Mar 3, 2016, 3:19:24 AM3/3/16
to wcm-i...@googlegroups.com
btw., if you think it would be useful to have direct support for those OSGi services in AEM mocks we can add them.
please crate a JIRA issue in this case.
patches are welcome as well...

stefan

>-----Original Message-----
>From: wcm-i...@googlegroups.com [mailto:wcm-i...@googlegroups.com] On
>Behalf Of Alex Theys
>Sent: Wednesday, March 02, 2016 1:40 PM
>To: wcm-io Developers
>--
>You received this message because you are subscribed to the Google Groups
>"wcm-io Developers" group.
>To unsubscribe from this group and stop receiving emails from it, send an
>email to wcm-io-dev+...@googlegroups.com.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/wcm-io-dev/dea23861-e1e4-44b2-9a00-
>af4732d85d43%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages