Hi All,
I created a junit class for a service and while running, getting an exception as below.
I am getting resource resolver as below. Only then I am able to adapt that to session in later step. Inside serviceclass, I close resource resolver that was created in service.
when(resourceUtility.getResourceResolver(Mockito.any(ResourceResolverFactory.class))).thenReturn(context.resourceResolver());
If I return mocked resource resolver as below, resolver.adaptto(Session.class) is returning as null.
when(resourceUtility.getResourceResolver(Mockito.any(ResourceResolverFactory.class))).thenReturn(resourceResolver);
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @ataemuser ,
You can try this way
@Mock private ResourceResolverFactory resourceResolverFactory; @Mock private ResourceResolver resourceResolver; @Mock private Session session; when(resourceResolverFactory.getThreadResourceResolver()).thenReturn(resourceResolver); when(resourceResolver.adaptTo(Session.class)).thenReturn(session);
@ataemuser This exception occurs when you you are using the resourceresolver after closing it. Please check if it is closed earlier and then used. |
Even if you adapting any other object from resourceresolver and meanwhile you might be closing the resourceresolver.
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
If you are adapting anything like shown above. Close your resourceresolver after the work for the adapted class is over
Thanks
Hi shaileshbassi,
Exception is not thrown inside service class. It is inside test class inside a tearDown method. Please refer the screenshot attached. It doesn't have any reference to custom class I wrote.
Thanks,
Akhila
Hi,
Can you try with Autoclosable try for resource resolver in your main class, no need to explicitly closing?
Hi Arun,
As I mentioned in above reply, this will not fix my issue. Service is working as expected. The problem is when I use resolver from aemContext in junit. As we close resolver in service, after executing test method, inside tearDown method of test class, its throwing exception as resolver closed. If you see the error snapshot attached, exception is not in service, but some OOB class related to unit test.
Try with below
private Session session; session = mock(Session.class); when(resource.getResourceResolver().adaptTo(Session.class)).thenReturn(session);
Hi @ataemuser ,
You can try this way
@Mock private ResourceResolverFactory resourceResolverFactory; @Mock private ResourceResolver resourceResolver; @Mock private Session session; when(resourceResolverFactory.getThreadResourceResolver()).thenReturn(resourceResolver); when(resourceResolver.adaptTo(Session.class)).thenReturn(session);
Views
Like
Replies
Views
Likes
Replies