import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
-import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({System.class, FrameworkUtil.class})
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ConfigPropertiesTest {
private static final String TEST_PROPERTY_KEY1 = "foobar34465$3467";
@Test
public void testGetProperty() {
- Bundle bundleWithoutBundleNoContext = mock(Bundle.class);
- Bundle bundle = mock(Bundle.class);
+ try (var frameworkUtil = mockStatic(FrameworkUtil.class)) {
+ final var bundleWithoutBundleNoContext = mock(Bundle.class);
+ final var bundle = mock(Bundle.class);
- // mock #1
- PowerMockito.mockStatic(FrameworkUtil.class);
- PowerMockito.when(FrameworkUtil.getBundle(this.getClass())).thenReturn(bundle);
- PowerMockito.when(FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class))
+ // mock #1
+ frameworkUtil.when(() -> FrameworkUtil.getBundle(getClass()))
+ .thenReturn(bundle);
+ frameworkUtil.when(() -> FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class))
.thenReturn(bundleWithoutBundleNoContext);
- // mock #2
- BundleContext bundleContext = mock(BundleContext.class);
- when(bundle.getBundleContext()).thenReturn(bundleContext);
- when(bundleWithoutBundleNoContext.getBundleContext()).thenReturn(null);
- // mock #3
- when(bundleContext.getProperty(eq(TEST_PROPERTY_KEY1))).thenReturn(TEST_PROPERTY_MOCK_VALUE1);
- // mock #4
- // PowerMockito.mockStatic(System.class);
- // PowerMockito.when(System.getProperty(eq(TEST_PROPERTY_KEY2))).thenReturn(TEST_PROPERTY_MOCK_VALUE2);
- // NOTE: The mock #4 above is not supported by PowerMockito. To work around this limitation,
- // we will simply add the property explicitly into System (instead of the solution mentioned in link below).
- // See: http://javax0.wordpress.com/2013/01/29/how-to-mock-the-system-class/
- System.setProperty(TEST_PROPERTY_KEY2, TEST_PROPERTY_MOCK_VALUE2);
-
- // test 1. bundle is null, returned from a mock
- assertNull(FrameworkUtil.getBundle(ConfigPropertiesTestMocking.class));
- assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTest.class), bundle);
- assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class),
+
+ // mock #2
+ BundleContext bundleContext = mock(BundleContext.class);
+ when(bundle.getBundleContext()).thenReturn(bundleContext);
+ when(bundleWithoutBundleNoContext.getBundleContext()).thenReturn(null);
+ // mock #3
+ when(bundleContext.getProperty(TEST_PROPERTY_KEY1)).thenReturn(TEST_PROPERTY_MOCK_VALUE1);
+ when(bundleContext.getProperty(TEST_PROPERTY_KEY2)).thenReturn(null);
+ when(bundleContext.getProperty(TEST_PROPERTY_KEY_NOT_FOUND)).thenReturn(null);
+
+ // mock #4, Mockito says:
+ // It is not possible to mock static methods of java.lang.System to avoid interfering with class loading
+ // what leads to infinite loops
+ //
+ // To work around this limitation, we will simply add the property explicitly into System
+ System.setProperty(TEST_PROPERTY_KEY2, TEST_PROPERTY_MOCK_VALUE2);
+
+ // test 1. bundle is null, returned from a mock
+ assertNull(FrameworkUtil.getBundle(ConfigPropertiesTestMocking.class));
+ assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTest.class), bundle);
+ assertEquals(FrameworkUtil.getBundle(ConfigPropertiesTestMockingBundleNoContext.class),
bundleWithoutBundleNoContext);
- // test 2. bundleContext is null
- assertNull(bundleWithoutBundleNoContext.getBundleContext());
- assertEquals(bundle.getBundleContext(), bundleContext);
+ // test 2. bundleContext is null
+ assertNull(bundleWithoutBundleNoContext.getBundleContext());
+ assertEquals(bundle.getBundleContext(), bundleContext);
- // test 3. value returned from bundleContext.getProperty() is null.
- // Then System.getProperty() is called and can return a valid value if key is found.
- final String value31 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY2);
- assertEquals(TEST_PROPERTY_MOCK_VALUE2, value31);
+ // test 3. value returned from bundleContext.getProperty() is null.
+ // Then System.getProperty() is called and can return a valid value if key is found.
+ assertEquals(TEST_PROPERTY_MOCK_VALUE2, ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY2));
- // test 4. value returned from ConfigProperties.getProperty is null
- final String value41 = ConfigProperties.getProperty(ConfigPropertiesTestMocking.class, TEST_PROPERTY_KEY1);
- assertNull(value41); // class has no bundle
- final String value42 = ConfigProperties.getProperty(ConfigPropertiesTestMockingBundleNoContext.class,
+ // test 4. value returned from ConfigProperties.getProperty is null
+ final String value41 = ConfigProperties.getProperty(ConfigPropertiesTestMocking.class, TEST_PROPERTY_KEY1);
+ assertNull(value41); // class has no bundle
+ final String value42 = ConfigProperties.getProperty(ConfigPropertiesTestMockingBundleNoContext.class,
TEST_PROPERTY_KEY1);
- assertNull(value42); // class has no bundleContext
- final String value43 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY_NOT_FOUND);
- assertNull(value43); // bundleContext will not know about key provided
-
- // test 5. value returned from ConfigProperties.getProperty is the default value provided
- final String value5 =
- ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY_NOT_FOUND, DEFAULT_PROPERTY_VALUE);
- assertEquals(DEFAULT_PROPERTY_VALUE, value5);
-
- // test 6. value returned from ConfigProperties.getProperty is the mocked value
- final String value61 = ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY1);
- assertEquals(TEST_PROPERTY_MOCK_VALUE1, value61);
- final String value62 =
- ConfigProperties.getProperty(this.getClass(), TEST_PROPERTY_KEY1, DEFAULT_PROPERTY_VALUE);
- assertEquals(TEST_PROPERTY_MOCK_VALUE1, value62);
+ assertNull(value42); // class has no bundleContext
+ final String value43 = ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY_NOT_FOUND);
+ assertNull(value43); // bundleContext will not know about key provided
+
+ // test 5. value returned from ConfigProperties.getProperty is the default value provided
+ assertEquals(DEFAULT_PROPERTY_VALUE,
+ ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY_NOT_FOUND, DEFAULT_PROPERTY_VALUE));
+
+ // test 6. value returned from ConfigProperties.getProperty is the mocked value
+ assertEquals(TEST_PROPERTY_MOCK_VALUE1, ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY1));
+ assertEquals(TEST_PROPERTY_MOCK_VALUE1,
+ ConfigProperties.getProperty(getClass(), TEST_PROPERTY_KEY1, DEFAULT_PROPERTY_VALUE));
+ }
}
// Helper classes used to de-mux mock behaviors
- private class ConfigPropertiesTestMockingBundleNoContext {
+ private static class ConfigPropertiesTestMockingBundleNoContext {
}
- private class ConfigPropertiesTestMocking {
+ private static class ConfigPropertiesTestMocking {
}
}