Dear all,
I have one simple service implementation class called ResourceResolverServiceImpl and I want to write JUNIT test class for this. I am very new to this JUNIT test.
************************* ResourceResolverServiceImpl Class *********************
import test.contentadmin.core.service.ResourceResolverService;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import java.util.HashMap;
import java.util.Map;
@Component(service = ResourceResolverService.class, immediate = true)
public class ResourceResolverServiceImpl implements ResourceResolverService {
private static final String SYSTEM_USER = "datawrite";
@reference
ResourceResolverFactory resourceResolverFactory;
@Override
public ResourceResolver getResourceResolver() throws LoginException {
Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, SYSTEM_USER);
return resourceResolverFactory.getServiceResourceResolver(param);
}
}
******************************** ResourceResolverService Class *********************
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
public interface ResourceResolverService {
public ResourceResolver getResourceResolver() throws LoginException;
}
Now I want to write the JUNIT for this...any good docs "or" video is there for this JUNIT ?
Please suggest.
Thanks & Regards
sub
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @subnaik, You can mock ResourceResolverFactory using MockResourceResolverFactory.
private AemContext ctx = new AemContext(
ResourceResolverType.RESOURCERESOLVER_MOCK);
private ResourceResolverServiceImpl resolverService;
@BeforeEach
void setUp() {
ctx.registerService(
ResourceResolverFactory.class,
new MockResourceResolverFactory());
resolverService = ctx.registerInjectActivateService(
new ResourceResolverServiceImpl());
}
Hi @subnaik, You can mock ResourceResolverFactory using MockResourceResolverFactory.
private AemContext ctx = new AemContext(
ResourceResolverType.RESOURCERESOLVER_MOCK);
private ResourceResolverServiceImpl resolverService;
@BeforeEach
void setUp() {
ctx.registerService(
ResourceResolverFactory.class,
new MockResourceResolverFactory());
resolverService = ctx.registerInjectActivateService(
new ResourceResolverServiceImpl());
}
Views
Like
Replies