Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Exception while running test class

Avatar

Level 2

Hi All,

 

I created a junit class for a service and while running, getting an exception as below.

 

akhilathomas1_0-1656442281065.png

 

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);

 

@Vijayalakshmi_S 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @akhilathomas1 ,

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);

View solution in original post

6 Replies

Avatar

Community Advisor

@akhilathomas1 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

Avatar

Level 2

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 

Avatar

Community Advisor

Hi,

Can you try with Autoclosable try for resource resolver in your main class, no need to explicitly closing?

https://cqdump.joerghoh.de/2018/11/14/try-with-resource-or-i-will-never-forget-to-close-a-resource-r... 



Arun Patidar

Avatar

Level 2

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.

Avatar

Community Advisor

Try with below

 

private Session session;
session = mock(Session.class);
when(resource.getResourceResolver().adaptTo(Session.class)).thenReturn(session);

 



Arun Patidar

Avatar

Correct answer by
Community Advisor

Hi @akhilathomas1 ,

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);