Ditch powermock from util.config 68/98268/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 2 Nov 2021 13:15:20 +0000 (14:15 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 2 Nov 2021 13:19:46 +0000 (14:19 +0100)
We have static mocking here, rework it in terms of Mockito.

Change-Id: I66b5afd16e8fd60e8331d81cc8af8ee64922839c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
utils/config/pom.xml
utils/config/src/test/java/org/opendaylight/ovsdb/utils/config/ConfigPropertiesTest.java
utils/config/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker [new file with mode: 0644]

index cebbd9df5ee3436ab8b86702f898b0a1878e20f1..30bbad5a7a06825f8ae6c32a79713b3e1eb75c3e 100644 (file)
@@ -29,16 +29,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <groupId>org.osgi</groupId>
       <artifactId>osgi.core</artifactId>
     </dependency>
-
-    <!-- testing dependencies -->
-    <dependency>
-      <groupId>org.powermock</groupId>
-      <artifactId>powermock-api-mockito2</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.powermock</groupId>
-      <artifactId>powermock-module-junit4</artifactId>
-    </dependency>
   </dependencies>
 
   <build>
index 9b9c770ea17797bc342332dd3ebb273c302716d6..3e3e0c07946f6a551efcb43400f72401027664b0 100644 (file)
@@ -9,21 +9,18 @@ package org.opendaylight.ovsdb.utils.config;
 
 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";
@@ -35,69 +32,70 @@ public class ConfigPropertiesTest {
 
     @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 {
     }
 }
diff --git a/utils/config/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/utils/config/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
new file mode 100644 (file)
index 0000000..1f0955d
--- /dev/null
@@ -0,0 +1 @@
+mock-maker-inline