Expand my Community achievements bar.

SOLVED

Facing Junits issue BundleContext bundleContext = FrameworkUtil.getBundle(X.class).getBundleContext();

Avatar

Level 2

Hi All,
I am facing some issues in writing Junits for this specific line BundleContext bundleContext = FrameworkUtil.getBundle(RunModeUtils.class).getBundleContext(); where I am getting nullPointerException and when I debug the code for getBundle() method, I observed cl is the reference of LauncherAppClassLoader and inside LauncherAppClassLoader  we have defaultdomain object where I am getting this error ProtectionDomain (null <no signer certificates>)
sun.misc.Launcher$AppClassLoader@18b4aac2
<no principals>
null

So, in the if statement cl is not reference of BundleReference and therefore giving value as Null. How to resolve this issue? Can you please suggest? 

public static Bundle getBundle(final Class<?> classFromBundle) {
// We use doPriv since the caller may not have permission
// to call getClassLoader.
Object cl = AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
return classFromBundle.getClassLoader();
}
});

if (cl instanceof BundleReference) {
return ((BundleReference) cl).getBundle();
}
return null;
}
1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi @nibedita07 ,

 

You don't need to try fix the code some other library has written. Simply mock the static class to save your time. With Junit5 you can mock the static class just make sure to update Jupiter dependency > 4.6.0. Sample code:

 

 

try (MockedStatic<YourStaticUtils> utilities = Mockito.mockStatic(YourStaticUtils.class)){         
        utilities.when(YourStaticUtils.getName()).thenReturn("Eugen");
        // Do whatever else needed
        assertThat(YourStaticUtils.getName()).isEqualTo("Eugen");
}

 

 

In case MockedStatic dependency not resolved, make sure to check this in your pom.xml:

<dependency>
    <groupId>org.mockito</groupId>
   <artifactId>mockito-junit-jupiter</artifactId>
   <version>4.6.1</version>
   <scope>test</scope>
</dependency>

View solution in original post

5 Replies

Avatar

Employee Advisor

What test framework do you use? Sling Mocks?

 

It seems to me that this code (not the test itself) is supposed to retrieve the OSGI bundle object based on a classname. Is this really necessary? Can't you have this code running in the context of an OSGI Component/Service, where you can get access to the bundle object in a much simpler and more robust way? In that case the tests are also much simpler to build.

Avatar

Level 2

Hi @Jörg_Hoh  I am using junit5 AemContextExtension here and do you mean I am supposed to modify the main class in context of OSGI component? if yes, it is an old code base where I am not supposed to change any old code by making it an OSGI component/service

Avatar

Correct answer by
Employee Advisor

Hi @nibedita07 ,

 

You don't need to try fix the code some other library has written. Simply mock the static class to save your time. With Junit5 you can mock the static class just make sure to update Jupiter dependency > 4.6.0. Sample code:

 

 

try (MockedStatic<YourStaticUtils> utilities = Mockito.mockStatic(YourStaticUtils.class)){         
        utilities.when(YourStaticUtils.getName()).thenReturn("Eugen");
        // Do whatever else needed
        assertThat(YourStaticUtils.getName()).isEqualTo("Eugen");
}

 

 

In case MockedStatic dependency not resolved, make sure to check this in your pom.xml:

<dependency>
    <groupId>org.mockito</groupId>
   <artifactId>mockito-junit-jupiter</artifactId>
   <version>4.6.1</version>
   <scope>test</scope>
</dependency>