Expand my Community achievements bar.

SOLVED

unit test resourceresolver : AEM JUnit throwing "Resourceresolver already closed" exception.

Avatar

Level 7

 

How to unit test below piece of code, without getting a "Resourceresolver already closed" exception.

I am using AemContext.resourceResolver() to get handle onto resource resolver and try with resource block closing resource resolver after unit of work complete.

 

How anyone is testing below piece of code

 

 

try (ResourceResolver resolver = factory.getServiceResourceResolver(authInfo)) {
    Resource resource = resolver.getResource("/content/sourcedcode/jcr:content");
    if (Objects.nonNull(resource)) {
        String prop = resource.getValueMao().get("key", String.class);
    }
} catch (Exception e) {
    e.printStackTrace();
}

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @sreenu539 

check this - 

try (ResourceResolver resolver = factory.getServiceResourceResolver(authInfo)) {
    Resource currentResource = resolver.getResource("/content/sourcedcode");
    Resource contentResource = currentResource.getChild("jcr:content");
    if (Objects.nonNull(contentResource)) {
        String prop = contentResource.getValueMap().get("key", String.class);
        // use the prop value here
    }
} catch (Exception e) {
    e.printStackTrace();
}

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @sreenu539 

check this - 

try (ResourceResolver resolver = factory.getServiceResourceResolver(authInfo)) {
    Resource currentResource = resolver.getResource("/content/sourcedcode");
    Resource contentResource = currentResource.getChild("jcr:content");
    if (Objects.nonNull(contentResource)) {
        String prop = contentResource.getValueMap().get("key", String.class);
        // use the prop value here
    }
} catch (Exception e) {
    e.printStackTrace();
}