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 :
Solved! Go to Solution.
Views
Replies
Total Likes
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 */ }
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 */ }
Views
Likes
Replies