Fix NotificationListenerInvokerTest on Java 19+ 64/108064/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 29 Sep 2023 16:03:18 +0000 (18:03 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 29 Sep 2023 16:06:36 +0000 (18:06 +0200)
We cannot mock MethodHandle, as it is a sealed class on Java 19+. Adjust
the test to use String.toString() as the source of the MH.

Change-Id: Ieab28fdec5c703c2fcad8b14cd95ea3f49bd2a21
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/NotificationListenerInvokerTest.java

index 4d11ed0388ad8948f011052b01778f6e61c2e5fc..ee761fc4a2600c4962d756b5fc6061205f65c945 100644 (file)
@@ -7,16 +7,16 @@
  */
 package org.opendaylight.mdsal.binding.dom.adapter.invoke;
 
+import static org.hamcrest.CoreMatchers.endsWith;
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThrows;
 import static org.mockito.Mockito.mock;
 
 import com.google.common.collect.ImmutableMap;
 import com.google.common.util.concurrent.UncheckedExecutionException;
-import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
 import java.lang.invoke.WrongMethodTypeException;
 import org.junit.Test;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OpendaylightTestNotificationListener;
@@ -42,16 +42,17 @@ public class NotificationListenerInvokerTest {
     }
 
     @Test
-    public void invokeNotification() {
-        final NotificationListener notificationListener = mock(NotificationListener.class);
-        final MethodHandle methodHandle = mock(MethodHandle.class);
-        final NotificationListenerInvoker notificationListenerInvoker =
-                new NotificationListenerInvoker(ImmutableMap.of(QName.create("test", "test"), methodHandle));
+    public void invokeNotification() throws Exception {
+        final var notificationListener = mock(NotificationListener.class);
+        final var methodHandle = MethodHandles.publicLookup().unreflect(String.class.getDeclaredMethod("toString"));
+
+        final var notificationListenerInvoker = new NotificationListenerInvoker(
+            ImmutableMap.of(QName.create("test", "test"), methodHandle));
 
         final var ex = assertThrows(WrongMethodTypeException.class,
             () -> notificationListenerInvoker.invokeNotification(notificationListener, QName.create("test", "test"),
                 null));
-        assertEquals("expected null but found (NotificationListener,DataContainer)void", ex.getMessage());
+        assertThat(ex.getMessage(), endsWith(" (String)String but found (NotificationListener,DataContainer)void"));
     }
 
     private interface TestPrivateInterface extends NotificationListener, Augmentation {