OSGiTools implements OSGiToolsMXBean { @Override public String getResourceURL(long bundleID, String resource) { Bundle b = bundleContext.getBundle(bundleID); if (b == null) throw new IllegalArgumentException("Not a valid bundle ID: " + bundleID); URL res = b.getResource(resource); if (res == null) return null; else return res.toString(); } OSGiTools(BundleContext bc); @Override long getLoadClassOrigin(long bundleID, String clazz); @Override String getResourceURL(long bundleID, String resource);  }
@Test public void testGetResourceURL() { URL someURL = getClass().getClassLoader().getResource( getClass().getName().replace('.', '/') + ".class"); Bundle bundle = Mockito.mock(Bundle.class); Mockito.when(bundle.getResource("org/foo/Bar.txt")).thenReturn(someURL); BundleContext bc = Mockito.mock(BundleContext.class); Mockito.when(bc.getBundle(42)).thenReturn(bundle); OSGiTools tools = new OSGiTools(bc); Assert.assertEquals(someURL.toString(), tools.getResourceURL(42L, "org/foo/Bar.txt")); Assert.assertNull(tools.getResourceURL(42L, "org/foo/Bar.txt.doesntexist")); try { tools.getResourceURL(7, "nonexistent.resource"); Assert.fail("Should have thworn an IllegalArgumentException, bundle 7 does not exist"); } catch (IllegalArgumentException iae) { } }