MockInvoker implements Invoker<T> { @SuppressWarnings("unchecked") public static Object getMockObject(String mockService, Class serviceType) { boolean isDefault = ConfigUtils.isDefault(mockService); if (isDefault) { mockService = serviceType.getName() + "Mock"; } Class<?> mockClass; try { mockClass = ReflectUtils.forName(mockService); } catch (Exception e) { if (!isDefault) { 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()); } try { return mockClass.newInstance(); } catch (InstantiationException e) { throw new IllegalStateException("No default constructor from mock class " + mockClass.getName(), e); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } } MockInvoker(URL url, Class<T> type); static Object parseMockValue(String mock); static Object parseMockValue(String mock, Type[] returnTypes); @Override Result invoke(Invocation invocation); static Throwable getThrowable(String throwstr); @SuppressWarnings("unchecked") static Object getMockObject(String mockService, Class serviceType); static String normalizeMock(String mock); @Override URL getUrl(); @Override boolean isAvailable(); @Override void destroy(); @Override Class<T> getInterface();  }
@Test public void testGetMockObject() { Assertions.assertEquals("", MockInvoker.getMockObject("java.lang.String", String.class)); Assertions.assertThrows(IllegalStateException.class, () -> MockInvoker .getMockObject("true", String.class)); Assertions.assertThrows(IllegalStateException.class, () -> MockInvoker .getMockObject("default", String.class)); Assertions.assertThrows(IllegalStateException.class, () -> MockInvoker .getMockObject("java.lang.String", Integer.class)); Assertions.assertThrows(IllegalStateException.class, () -> MockInvoker .getMockObject("java.io.Serializable", Serializable.class)); }