Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list
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

0 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

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

 

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

The ultimate experience is back.

Join us in Vegas to build skills, learn from the world's top brands, and be inspired.

Register Now