Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

need some lights for JUNIT test for ResourceResolverService Implementation class

Avatar

Level 4

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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());
}

 

Reference: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-register-resource-r... 

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

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());
}

 

Reference: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-register-resource-r...