MockInvoker implements Invoker<T> { @Override public Result invoke(Invocation invocation) throws RpcException { if (invocation instanceof RpcInvocation) { ((RpcInvocation) invocation).setInvoker(this); } String mock = null; if (getUrl().hasMethodParameter(invocation.getMethodName())) { mock = getUrl().getParameter(invocation.getMethodName() + "." + MOCK_KEY); } if (StringUtils.isBlank(mock)) { mock = getUrl().getParameter(MOCK_KEY); } if (StringUtils.isBlank(mock)) { throw new RpcException(new IllegalAccessException("mock can not be null. url :" + url)); } mock = normalizeMock(URL.decode(mock)); if (mock.startsWith(RETURN_PREFIX)) { mock = mock.substring(RETURN_PREFIX.length()).trim(); try { Type[] returnTypes = RpcUtils.getReturnTypes(invocation); Object value = parseMockValue(mock, returnTypes); return AsyncRpcResult.newDefaultAsyncResult(value, invocation); } catch (Exception ew) { throw new RpcException("mock return invoke error. method :" + invocation.getMethodName() + ", mock:" + mock + ", url: " + url, ew); } } else if (mock.startsWith(THROW_PREFIX)) { mock = mock.substring(THROW_PREFIX.length()).trim(); if (StringUtils.isBlank(mock)) { throw new RpcException("mocked exception for service degradation."); } else { Throwable t = getThrowable(mock); throw new RpcException(RpcException.BIZ_EXCEPTION, t); } } else { try { Invoker<T> invoker = getInvoker(mock); return invoker.invoke(invocation); } catch (Throwable t) { throw new RpcException("Failed to create mock implementation class " + mock, t); } } } 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 testInvoke() { URL url = URL.valueOf("remote: url = url.addParameter(MOCK_KEY, "return "); MockInvoker mockInvoker = new MockInvoker(url, String.class); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Assertions.assertEquals(new HashMap<>(), mockInvoker.invoke(invocation).getObjectAttachments()); }
@Test public void testInvokeThrowsRpcException1() { URL url = URL.valueOf("remote: MockInvoker mockInvoker = new MockInvoker(url, null); Assertions.assertThrows(RpcException.class, () -> mockInvoker.invoke(new RpcInvocation())); }
@Test public void testInvokeThrowsRpcException2() { URL url = URL.valueOf("remote: url = url.addParameter(MOCK_KEY, "fail"); MockInvoker mockInvoker = new MockInvoker(url, String.class); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Assertions.assertThrows(RpcException.class, () -> mockInvoker.invoke(invocation)); }
@Test public void testInvokeThrowsRpcException3() { URL url = URL.valueOf("remote: url = url.addParameter(MOCK_KEY, "throw"); MockInvoker mockInvoker = new MockInvoker(url, String.class); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Assertions.assertThrows(RpcException.class, () -> mockInvoker.invoke(invocation)); }