Skip to content

Commit

Permalink
Support spring bean as mock value (apache#6188)
Browse files Browse the repository at this point in the history
* support mock impl as spring bean
  • Loading branch information
chickenlj authored May 27, 2020
1 parent bd6b580 commit 7325783
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.rpc.support;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionFactory;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.common.utils.ConfigUtils;
Expand Down Expand Up @@ -177,12 +178,29 @@ private Invoker<T> getInvoker(String mockService) {

@SuppressWarnings("unchecked")
public static Object getMockObject(String mockService, Class serviceType) {
if (ConfigUtils.isDefault(mockService)) {
boolean isDefault = ConfigUtils.isDefault(mockService);
if (isDefault) {
mockService = serviceType.getName() + "Mock";
}

Class<?> mockClass = ReflectUtils.forName(mockService);
if (!serviceType.isAssignableFrom(mockClass)) {
Class<?> mockClass;
try {
mockClass = ReflectUtils.forName(mockService);
} catch (Exception e) {
if (!isDefault) {// does not check Spring bean if it is default config.
ExtensionFactory extensionFactory =
ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension();
Object obj = extensionFactory.getExtension(serviceType, mockService);
if (obj != null) {
return obj;
}
}
throw new IllegalStateException("Did not find mock class or instance "
+ mockService
+ ", please check if there's mock class or instance implementing interface "
+ serviceType.getName(), e);
}
if (mockClass == null || !serviceType.isAssignableFrom(mockClass)) {
throw new IllegalStateException("The mock class " + mockClass.getName() +
" not implement interface " + serviceType.getName());
}
Expand Down

0 comments on commit 7325783

Please sign in to comment.