You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/05/26 05:13:06 UTC

[camel] branch main updated: CAMEL-16658: camel-bean: AmbiguousMethodCallException when using Mockito mock as bean for camel-bean component. Thanks to Krzysztof Mackowiak for the patch.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new b35cff0  CAMEL-16658: camel-bean: AmbiguousMethodCallException when using Mockito mock as bean for camel-bean component. Thanks to Krzysztof Mackowiak for the patch.
b35cff0 is described below

commit b35cff02b6b36159c584efdaf227312d3cee6007
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed May 26 06:52:42 2021 +0200

    CAMEL-16658: camel-bean: AmbiguousMethodCallException when using Mockito mock as bean for camel-bean component. Thanks to Krzysztof Mackowiak for the patch.
---
 .../org/apache/camel/component/bean/BeanInfo.java  |  7 +++
 .../component/bean/MockitoMockForClassTest.java    | 62 ++++++++++++++++++++++
 .../bean/MockitoMockForInterfaceTest.java          | 60 +++++++++++++++++++++
 .../component/bean/MockitoSpyForClassTest.java     | 62 ++++++++++++++++++++++
 4 files changed, 191 insertions(+)

diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java
index 39a5458..261e6e6 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java
@@ -64,6 +64,8 @@ import static org.apache.camel.component.bean.ParameterMappingStrategyHelper.cre
 public class BeanInfo {
     private static final Logger LOG = LoggerFactory.getLogger(BeanInfo.class);
     private static final String CGLIB_CLASS_SEPARATOR = "$$";
+    private static final String CGLIB_METHOD_MARKER = "CGLIB$";
+    private static final String BYTE_BUDDY_METHOD_MARKER = "$accessor$";
     private static final String[] EXCLUDED_METHOD_NAMES = new String[] {
             "clone", "equals", "finalize", "getClass", "hashCode", "notify", "notifyAll", "wait", // java.lang.Object
             "getInvocationHandler", "getProxyClass", "isProxyClass", "newProxyInstance" // java.lang.Proxy
@@ -918,6 +920,11 @@ public class BeanInfo {
             return false;
         }
 
+        // must not be a method added by Mockito (CGLIB or Byte Buddy)
+        if (name.contains(CGLIB_METHOD_MARKER) || name.contains(BYTE_BUDDY_METHOD_MARKER)) {
+            return false;
+        }
+
         return true;
     }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/MockitoMockForClassTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/MockitoMockForClassTest.java
new file mode 100644
index 0000000..b1c6a57
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/MockitoMockForClassTest.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.Registry;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.when;
+
+public class MockitoMockForClassTest extends ContextTestSupport {
+
+    @Test
+    public void testCallingMock() throws Exception {
+        Object response = template.requestBody("direct:start", "anything");
+        assertEquals("mocked answer", response);
+    }
+
+    @Override
+    protected Registry createRegistry() throws Exception {
+        MyService mockService = Mockito.mock(MyService.class);
+        when(mockService.doSomething(any())).thenReturn("mocked answer");
+
+        Registry answer = super.createRegistry();
+        answer.bind("myService", mockService);
+        return answer;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:start").bean("bean:myService");
+            }
+        };
+    }
+
+    public class MyService {
+        public String doSomething(String body) {
+            return "real answer";
+        }
+    }
+
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/MockitoMockForInterfaceTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/MockitoMockForInterfaceTest.java
new file mode 100644
index 0000000..464774c
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/MockitoMockForInterfaceTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.Registry;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.when;
+
+public class MockitoMockForInterfaceTest extends ContextTestSupport {
+
+    @Test
+    public void testCallingMock() throws Exception {
+        Object response = template.requestBody("direct:start", "anything");
+        assertEquals("mocked answer", response);
+    }
+
+    @Override
+    protected Registry createRegistry() throws Exception {
+        MyService mockService = Mockito.mock(MyService.class);
+        when(mockService.doSomething(any())).thenReturn("mocked answer");
+
+        Registry answer = super.createRegistry();
+        answer.bind("myService", mockService);
+        return answer;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:start").bean("bean:myService");
+            }
+        };
+    }
+
+    public interface MyService {
+        String doSomething(String body);
+    }
+
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/MockitoSpyForClassTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/MockitoSpyForClassTest.java
new file mode 100644
index 0000000..4918782
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/MockitoSpyForClassTest.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.Registry;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.when;
+
+public class MockitoSpyForClassTest extends ContextTestSupport {
+
+    @Test
+    public void testCallingSpy() throws Exception {
+        Object response = template.requestBody("direct:start", "anything");
+        assertEquals("mocked answer", response);
+    }
+
+    @Override
+    protected Registry createRegistry() throws Exception {
+        MyService mockService = Mockito.spy(new MyService());
+        when(mockService.doSomething(any())).thenReturn("mocked answer");
+
+        Registry answer = super.createRegistry();
+        answer.bind("myService", mockService);
+        return answer;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:start").bean("bean:myService");
+            }
+        };
+    }
+
+    public class MyService {
+        public String doSomething(String body) {
+            return "real answer";
+        }
+    }
+
+}