Facing Junits issue BundleContext bundleContext = FrameworkUtil.getBundle(X.class).getBundleContext(); | Community
Skip to main content
Employee
Mar 29, 2023 7:23 AM UTC
Solved

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

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;
}
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
vivekanand-mishAccepted solution
Employee
Mar 31, 2023 3:10 AM UTC

Hi @nibeditana ,

 

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>

5 replies

vivekanand-mishAccepted solution
Employee
Mar 31, 2023 3:10 AM UTC

Hi @nibeditana ,

 

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>

Translate
Employee
Mar 31, 2023 4:20 AM UTC

Hi @vivekanand-mish this approach works, thank you for your suggestion 🙂

Translate
joerghoh
Employee
Mar 30, 2023 11:28 AM UTC

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.

Translate
Employee
Mar 30, 2023 11:38 AM UTC

Hi @joerghoh  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

Translate