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

Junit for getting Configuration through Bundle Context


Level 2

I am trying to write Junit using Junit5


ProcessingComponentConfiguration config;

FrameworkUtil framework;

void setup() {

ConfigurationAdmin configAdmin;

ProcessingContext context;

BundleContext bundleContext;

SlingHttpServletRequest request;

void testInit() throws IOException {
Mockito.when( request.getServerName()).thenReturn("server");



It is giving null pointer exception when I inspect framework.getBundle(Test.class).getBundleContext().


How to write Junit for this. Below is my code.


BundleContext bundleContext = FrameworkUtil.getBundle(Test.class).getBundleContext();
ServiceReference slingSettingRef = bundleContext.getServiceReference(SlingSettingsService.class.getName());
SlingSettingsService slingSetting = (SlingSettingsService) bundleContext.getService(slingSettingRef);
ServiceReference factoryRef = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
ConfigurationAdmin configAdmin = (ConfigurationAdmin) bundleContext.getService(factoryRef);

1 Reply


Employee Advisor

I recommend you to use the AEM Mocks library; it provides you mock implementations for the OSGI basics and there you can create tests much more easily and convenient.


I wrote some blog posts about it, you can find them here: