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.
In Junit TestCase file the factory was not null ,PFB screenshot.
In SearchServiceImpl, its getting null. PFB screenshot.
my searchresultServlet, PFB
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
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.
and in My TestServlet doget() test method i updated as below
now im getting result as expected pfb,
Hi @rajat168 ,
You may try below code for AEM context instead of JCR_MOCK
private AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
Thanks
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.
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()));
}
}
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.
and in My TestServlet doget() test method i updated as below
now im getting result as expected pfb,
Views
Likes
Replies
Views
Likes
Replies