You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Robert Munteanu <ro...@apache.org> on 2020/02/05 22:37:23 UTC
Re: [mocks] Injecting references for a class with no SCR metadata?
Hello Stefan,
On Wed, 2020-01-29 at 10:56 +0000, Stefan Seifert wrote:
> hello robert.
>
> the SCR metadata is currently only found if the class name matches
> exactly. not sure if we should change the default behavior of osgi
> mocks here as this should be the same in a real OSGi behavior (like
> automatically looking up the parents).
You are right, I was trying to be too clever, and that usually
backfires. It's probably a bad idea to add some subtle difference.
> adding an explicit method to the helper methods
> org.apache.sling.testing.mock.osgi.MockOsgi which allows to pass in a
> reference to alternative set of SCR metadata for injecting the
> dependencies should do no harm.
I looked at the OsgiMetadata class and probably there is too much
baggage to make it easy to supply, as a consumer. Adding a DSL just for
that seems overkill. Also, adding a method to inject references based
on another class' metadata feels wrong.
>
> alternatively a mockito spy wrapper may help? but probably this leads
> to the same problem.
I guess so, but the current solution seems to be the least bad :-) What
I could also do is use constructor injection and manually inject the
references - at least that would work in unit tests as well.
Thanks,
Robert
>
> stefan
>
>
> > -----Original Message-----
> > From: Robert Munteanu [mailto:rombert@apache.org]
> > Sent: Wednesday, January 29, 2020 11:50 AM
> > To: dev@sling.apache.org
> > Subject: [mocks] Injecting references for a class with no SCR
> > metadata?
> >
> > Hi,
> >
> > I am working in the following (admittedly exotic) scenario:
> >
> > 1. An OSGi component with a number of dependencies
> > 2. A test that runs on a subclass of that method, overriding a
> > method
> > that I want to stub out locally
> >
> > I want to register the component and have the OSGi mocks library
> > inject
> > depedencies for references, maybe based on the SCR metadata of the
> > parent class? I could not find anything in the API for that.
> >
> > Alternatively, I could whip up a patch that checks if the the
> > argument
> > to registerInjectActivateService is an instance of an anonymous
> > class
> > and walks up the inheritance chain until the first non-anonymous
> > class
> > is found and uses that for SCR metadata.
> >
> > Thoughts?
> >
> > Thanks,
> > Robert
> >