Exception while running test class | Community
Skip to main content
New Participant
June 28, 2022
Solved

Exception while running test class

  • June 28, 2022
  • 4 replies
  • 1382 views

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.
Best answer by SantoshSai

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

4 replies

SantoshSai
SantoshSaiAccepted solution
New Participant
June 30, 2022

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
arunpatidar
New Participant
June 29, 2022

Try with below

 

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

 

Arun Patidar
arunpatidar
New Participant
June 29, 2022

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/ 

Arun Patidar
New Participant
June 29, 2022

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.

ShaileshBassi
New Participant
June 29, 2022

@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

New Participant
June 29, 2022

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