You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "Stefan Seifert (Jira)" <ji...@apache.org> on 2023/04/18 13:13:00 UTC

[jira] [Commented] (SLING-11823) Sling Mocks: Allow specifying service ranking (or additional properties) when calling registerAdapter()

    [ https://issues.apache.org/jira/browse/SLING-11823?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17713585#comment-17713585 ] 

Stefan Seifert commented on SLING-11823:
----------------------------------------

registerAdapter is a convenience method which currently registers adapters with highest-possible ranking, see https://github.com/apache/sling-org-apache-sling-testing-sling-mock/blob/acb57ff81631e7a7f9ef9e9bd0b5332b62e06c91/core/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java#L552-L554

usually this is what you want if you register a custom adapter factory for a single unit test. if you need more control, you can register the adapter factory manually with whatever service ranking desired.

that it's not working in your example has probably a different reason: if the class you call adapTo() on directly implements the adaption, the adapter manager and adapter factories are never called.

> Sling Mocks: Allow specifying service ranking (or additional properties) when calling registerAdapter()
> -------------------------------------------------------------------------------------------------------
>
>                 Key: SLING-11823
>                 URL: https://issues.apache.org/jira/browse/SLING-11823
>             Project: Sling
>          Issue Type: Improvement
>          Components: Testing
>    Affects Versions: Testing Sling Mock 3.4.4
>            Reporter: Henry Kuijpers
>            Priority: Major
>
> {code:java}
> final Session mockedSession = mock(Session.class);
> doThrow(new RepositoryException("Failure")).when(mockedSession).move("/content/test", "/content/test2");
> context.registerAdapter(ResourceResolver.class, Session.class, mockedSession);
> // Call implementation that uses the ResourceResolver and adapts it to Session to call move
> underTest.doSomething();
> {code}
> The consequence of this is that an adapterfactory is registered etc, however, it will not be called, since it has a lower service ranking than existing adapters (in case of JCR/OAK ResourceResolverType). 
> It would be nice to be able to specify additional properties, or at least be able to specify the service ranking, so that one of those adapters could be forced to be the first in the list.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)