Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

How To register resource resolver factory mock in aem context

Avatar

Level 3

Hi There,

 

I have a service say "Simple.java" that has an @reference for "ResourceResolverFactory". And a JUnit test class for the same service says "SimpleTest.java".

 

While registering service "Simple.java" with aem context it is giving resourceResolverFactory null even though we have created a mock for the same. And while trying to register the resolver factory mock we are having an issue called "No SCR metadata found for the class".

Please do let me know if you have any suggestions on that note. Also please take a look on the below sample code.

 

Simple.java

@Component(service = SimpleService.class, immediate = true)
public class SimpleServiceImpl implements SimpleService {

private static final Logger logger = LoggerFactory.getLogger(SimpleServiceImpl.class);

@Reference
private ResourceResolverFactory rsf;

@Override
public ResourceResolver simpleMethod(){
return rsf.getResourceResolver(null);
}

}

 

SimpleTest.java

@ExtendWith({ AemContextExtension.class, MockitoExtension.class})
class SimpleServiceImplTest {
private AemContext ctx = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
private SimpleServiceImpl simpleService;
@Mock
ResourceResolverFactory resourceResolverFactory;

@BeforeEach
void setUp() {
//throwing "No OSGi SCR metadata found for class org.apache.sling.api.resource.ResourceResolverFactory"
ctx.registerInjectActivateService(resourceResolverFactory);

simpleService = ctx.registerInjectActivateService(new simpleServiceImpl());
}

/*Some test cases */

}

 Error Snapshot :

Screenshot (350).png

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @ravi_joshi,

Could you please try to use MockResourceResolverFactory?

Your code look like this:

import org.apache.sling.testing.resourceresolver.MockResourceResolverFactory;

@ExtendWith({ AemContextExtension.class, MockitoExtension.class})
class SimpleServiceImplTest {
   private AemContext ctx = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
   private SimpleServiceImpl simpleService;
   
  // do not need below
  // @Mock
  // ResourceResolverFactory resourceResolverFactory;

   @BeforeEach
   void setUp() {
      // fix for "No OSGi ..."
      ctx.registerService(ResourceResolverFactory.class, new MockResourceResolverFactory());
 
      simpleService = ctx.registerInjectActivateService(new SimpleServiceImpl());
   }

/*Some test cases */

} 

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @ravi_joshi,

Could you please try to use MockResourceResolverFactory?

Your code look like this:

import org.apache.sling.testing.resourceresolver.MockResourceResolverFactory;

@ExtendWith({ AemContextExtension.class, MockitoExtension.class})
class SimpleServiceImplTest {
   private AemContext ctx = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
   private SimpleServiceImpl simpleService;
   
  // do not need below
  // @Mock
  // ResourceResolverFactory resourceResolverFactory;

   @BeforeEach
   void setUp() {
      // fix for "No OSGi ..."
      ctx.registerService(ResourceResolverFactory.class, new MockResourceResolverFactory());
 
      simpleService = ctx.registerInjectActivateService(new SimpleServiceImpl());
   }

/*Some test cases */

} 

 

Avatar

Level 3

Hey @lukasz-m ,

 

Thank you for your help. I have used mockResourceResolver as you suggested and it worked.