How to register resource mapping configuration to aem context? | Community
Skip to main content
Level 2
August 27, 2024

How to register resource mapping configuration to aem context?

  • August 27, 2024
  • 2 replies
  • 862 views

I have  a model where i am obtaining resource resolver from resourceresolverFactory . This model is a generic model referenced in multiple classes 

In one such test case , i want resolver.map("path") to give me the resolved paths according to my configurations. Is there a way to register these configurations to the aem context so that when resolver.map() is called it maps url correctly

2 replies

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
August 27, 2024

Hi, 

You could try to mock the resourceResolver object so you can return the expected value when resolver.map() is invoked. Try something like this:

@Mock private ResourceResolverFactory resourceResolverFactory; private ResourceResolver mockResolver; private AemContext context = new AemContext(ResourceResolverType.JCR_MOCK); @BeforeEach void setUp() throws LoginException { MockitoAnnotations.openMocks(this); context.registerService(ResourceResolverFactory.class, resourceResolverFactory); mockResolver = context.resourceResolver().clone(null); when(resourceResolverFactory.getServiceResourceResolver(any())).thenReturn(mockResolver); }

 

Hope this helps

Esteban Bustamante
ZendarkkeAuthor
Level 2
August 27, 2024

I mainly need help in adding mappings to my resource resolver factory / resolver for my context,

I have code where using resourceResolverFactory.getServiceResourceResolver(map);
link=resolver.map(link);

resolver is getting mocked but due to no mappings its not mapping my link , i need the link to be mapped for accurate test anyway to set this mapping to the context

kautuk_sahni
Community Manager
Community Manager
September 13, 2024

@zendarkke Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni