Junits for FrameworkUtil and BundleContext
We have a common Utility class, which has following method
public static ResourceResolver getServiceResourceResolver(String subServiceName){
final BundleContext bundleContext = FrameworkUtil.getBundle(Util.class).getBundleContext();
final ServiceReference factoryReference = bundleContext
.getServiceReference(ResourceResolverFactory.class.getName());
final ResourceResolverFactory resolverFactory = (ResourceResolverFactory) bundleContext
.getService(factoryReference);
final Map<String, Object> map = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE,
(Object) serviceName);
- return resolverFactory.getServiceResourceResolver(map);
}
This common Utility returns the resolver for AEMService :
resolver = Util.getServiceResourceResolver("user");
While writing Junits for the AEMService, the resolver is always null, because JUnit tests run in a plain JVM with no OSGi Framework. Therefore there is no BundleContext available.
How do we write Junit for the AEMService.java