AEMContextImpl throws exception in teardown method - junit

padmaja_seshadri

08-10-2020

try (ResourceResolver writeAccessResourceResolver = userMapperService.getJCRWriterService();
ResourceResolver readAccessResourceResolver = userMapperService.getJCRReadService()) {

...

 I am mocking userMapperService here , and returning aemContext.resourceResolver(), when  userMapperService.getJCRWriterService() or userMapperService.getJCRReadService() is called. 

 

My resourceresolver gets closed after the try block . But after the test case runs , it throws exception - " Resource Resolver is already closed " , nearAemContextImpl.tearDown method. 

 

java.lang.IllegalStateException: Resource resolver is already closed.

at org.apache.sling.resourceresolver.impl.ResourceResolverImpl.checkClosed(ResourceResolverImpl.java:186)
at org.apache.sling.resourceresolver.impl.ResourceResolverImpl.adaptTo(ResourceResolverImpl.java:804)
at org.apache.sling.testing.mock.sling.context.SlingContextImpl.tearDown(SlingContextImpl.java:216)
at io.wcm.testing.mock.aem.context.AemContextImpl.tearDown(AemContextImpl.java:99)
at io.wcm.testing.mock.aem.junit5.AemContext.tearDownContext(AemContext.java:106)
at io.wcm.testing.mock.aem.junit5.AemContextExtension.lambda$afterEach$2(AemContextExtension.java:115)
at io.wcm.testing.mock.aem.junit5.AemContextExtension.applyAemContext(AemContextExtension.java:123)
at io.wcm.testing.mock.aem.junit5.AemContextExtension.afterEach(AemContextExtension.java:110)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAfterEachCallbacks$11(TestMethodTestDescriptor.java:245)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$12(TestMethodTestDescriptor.java:256)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$13(TestMethodTestDescriptor.java:256)

 

How to fix this issue? 

Accepted Solutions (1)

Accepted Solutions (1)

Answers (1)

Answers (1)

Manjunathh

09-10-2020

Hi @padmaja_seshadri,

Can you please add screenshot with highlighting the line of code where this exception is getting thrown when JUnit test runs, it will be helpful to understand/reproduce the issue which you are getting.