Expand my Community achievements bar.

SOLVED

resourceResolverFactory getting null in serviceImpl class while writing Junit.

Avatar

Level 5

Hi team,

 we are on AEMCS ENV, we were trying to write test case for a search servlet. when we execute the search servlet doget method from servlet its executing successfully but in SearchserviceImpl.java Class the @Mock private ResourceResolverFactory resourceResolverFactory getting null. Kindly please suggest on this. please find the code snippet.

rajat168_0-1725025119559.png

In Junit TestCase file the factory was not null ,PFB screenshot.

rajat168_1-1725025236404.png

rajat168_2-1725025337969.png

 

In SearchServiceImpl, its getting null. PFB screenshot.

rajat168_3-1725025408865.png

 

my searchresultServlet, PFB

rajat168_4-1725025534709.png

 

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 5

Hi All,

    Thanks for your response, now i was able to mock the resourceResolverFactory;now the way,i used was below

updated the code in searchserviceImpl.class as below.

rajat168_0-1725085857779.png

and in My TestServlet  doget() test method i updated as below

rajat168_1-1725085949796.png

now im getting result as expected pfb,

rajat168_2-1725086019371.png

 

 

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @rajat168 ,

You may try below code for AEM context instead of JCR_MOCK

  private AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit5-and-mockito-tests-n...

 

Thanks

Avatar

Community Advisor

You are already using AEM Mocks, so there are many mock objects/statements which you don't really need (session, page, request, response, page etc.)
Most of these you can just get from the AEM context. For e.g you can remove the mock request and mock response and instead can directly rely on context.request() and context.response()
Try calling your doGet with these like
searchResultServlet.doGet(context.request(), context.response())
and move forward from there.
Hope this helps.

Avatar

Level 5

Hi @rajat168,

 

Actually, I would suggest to write custom stub (mock) class for ResourceResolverFactoryStub that can help you to test AEM code.

 

 

package com.common.resource;

import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.testing.resourceresolver.MockResourceResolverFactory;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

public class ResourceResolverFactoryStub extends MockResourceResolverFactory {
    private ResourceResolver resolver;
    private boolean isResourceResolverNull;
    private boolean isResourceResolverLive;
    private boolean exception;

    public ResourceResolverFactoryStub(ResourceResolver resolver) {
        super();
        this.resolver = resolver;
    }

    public ResourceResolverFactoryStub(ResourceResolver resolver, boolean isResourceResolverNull) {
        super();
        this.resolver = resolver;
        this.isResourceResolverNull = isResourceResolverNull;
    }

    public ResourceResolverFactoryStub(boolean isResourceResolverLive, boolean isResourceResolverNull) {
        super();
        this.isResourceResolverLive = isResourceResolverLive;
        this.isResourceResolverNull = isResourceResolverNull;
    }

    public ResourceResolverFactoryStub(boolean exception) {
        super();
        this.exception = exception;
    }

    @Override
    public @NotNull ResourceResolver getResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
        return resolver;
    }

    @Override
    public @NotNull ResourceResolver getAdministrativeResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
        return resolver;
    }

    @Override
    public @NotNull ResourceResolver getServiceResourceResolver(Map<String, Object> authenticationInfo) throws LoginException {
        if (exception) {
            throw new LoginException("");
        }
        if (isResourceResolverNull) {
            return null;
        }
        return resolver != null ? resolver : new ResourceResolverStub(this, isResourceResolverLive);
    }

    @Override
    public ResourceResolver getThreadResourceResolver() {
        return resolver;
    }
}

 

 Usages examples:

Test for OSGi component with constructor injection:

 

@ExtendWith(AemContextExtension.class)
class SearchServiceImplTest {

    private AemContext aemContext = AppAemContext.newAemContext();
    private ResourceResolverFactoryStub resourceResolverFactoryStub;
    private SearchServiceImpl underTest;

    @BeforeEach
    public void setUp() throws NoSuchMethodException {
        resourceResolverFactoryStub = new ResourceResolverFactoryStub(aemContext.resourceResolver());
        underTest = new SearchServiceImpl(resourceResolverFactoryStub);
    }
}

 

Testing of a Sling Model:

 

@ExtendWith(AemContextExtension.class)
class SomeModelTest {

    private final AemContext context = AppAemContext.newAemContextBuilder(ResourceResolverType.JCR_MOCK).build();

    @BeforeEach
    void setUp() {
        context.registerService(ResourceResolverFactory.class, new ResourceResolverFactoryStub(context.resourceResolver()));
    }
}

 

 

Avatar

Correct answer by
Level 5

Hi All,

    Thanks for your response, now i was able to mock the resourceResolverFactory;now the way,i used was below

updated the code in searchserviceImpl.class as below.

rajat168_0-1725085857779.png

and in My TestServlet  doget() test method i updated as below

rajat168_1-1725085949796.png

now im getting result as expected pfb,

rajat168_2-1725086019371.png