Exception while running test class | Community
Skip to main content
Level 2
Jun 28, 2022 6:58 PM UTC
Solved

Exception while running test class

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

 

@vijayalakshmi_s 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SantoshSai
SantoshSaiAccepted solution
New Participant
Jun 30, 2022 3:54 AM UTC

Hi @iamaemuser ,

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

6 replies

SantoshSai
SantoshSaiAccepted solution
New Participant
Jun 30, 2022 3:54 AM UTC

Hi @iamaemuser ,

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);
Translate
Santosh Sai
arunpatidar
New Participant
Jun 29, 2022 5:29 PM UTC

Try with below

 

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

 

Translate
Arun Patidar
arunpatidar
New Participant
Jun 29, 2022 7:29 AM UTC

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-resolver/ 

Translate
Arun Patidar
Level 2
Jun 29, 2022 1:08 PM UTC

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.

Translate
ShaileshBassi
New Participant
Jun 29, 2022 4:27 AM UTC

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

Translate
Level 2
Jun 29, 2022 4:30 AM UTC

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 

Translate