### Question:
RpcCaller { public RpcResult call(RpcCall rpcCall, ExceptionMarshaller exceptionSerializer, JsonHelper jsonHelper, List<RpcService> rpcServices, HttpServletRequest request) { Optional<Cookie> csrfProtectionCookie = HttpCookieUtils.getCookie(CSRF_PROTECTION_COOKIE_NAME, request); if(!csrfProtectionCookie.isPresent()) { throw new MissingCsrfProtectionCookieException(String.format("Can't find a CSRF protection cookie " + "named '%s'!", CSRF_PROTECTION_COOKIE_NAME)); } else if(!csrfProtectionCookie.get().getValue().equals(rpcCall.getCsrfProtectionToken())) { throw new CsrfAttackException(String.format("A CSRF attack was detected while calling the method " + "'%s' of the service '%s'.", rpcCall.getMethodName(), rpcCall.getServiceName())); } for(RpcService rpcService : rpcServices) { Class serviceClass = rpcService.getClass(); if(serviceClass.getName().equals(rpcCall.getServiceName())) { for(Method method : serviceClass.getDeclaredMethods()) { if(method.getName().equals(rpcCall.getMethodName())) { List<Object> arguments = new ArrayList<>(); int argIndex = 0; for(Class<?> parameterType : method.getParameterTypes()) { if(parameterType.equals(HttpServletRequest.class)) { arguments.add(request); argIndex++; } else if(parameterType.equals(HttpSession.class)) { arguments.add(request.getSession()); argIndex++; } else if(argIndex < rpcCall.getArgumentsJsons().size()) { arguments.add(jsonHelper.fromJson(rpcCall.getArgumentsJsons().get(argIndex), parameterType)); argIndex++; } else { arguments.add(null); } } Object rpcResult; try { rpcResult = method.invoke(rpcService, arguments.toArray()); } catch (IllegalAccessException | IllegalArgumentException ex) { throw new IllegalStateException(String.format("An error occured while invoking the metohd " + "'%s' of the service '%s' via refelction.", rpcCall.getMethodName(), rpcCall.getServiceName()), ex); } catch(InvocationTargetException ex) { return new RpcResult(Status.EXCEPTION, exceptionSerializer.marshall(ex.getCause(), jsonHelper)); } return new RpcResult(Status.SUCCESS, jsonHelper.toJson(rpcResult, method.getReturnType())); } } throw new IllegalStateException("The service '" + rpcCall.getServiceName() + "' has no public method " + "named '" + rpcCall.getMethodName() + "'!"); } } throw new IllegalStateException("There's no registered service with the name '" + rpcCall.getServiceName() + "'!"); }  RpcResult call(RpcCall rpcCall, ExceptionMarshaller exceptionSerializer, JsonHelper jsonHelper, 
            List<RpcService> rpcServices, HttpServletRequest request); final static String CSRF_PROTECTION_COOKIE_NAME; }### Answer:
@Test public void regularCall() { RpcResult result = call(new RpcCall(TestService.class.getName(), TestService.GET_BY_ID_METHOD_NAME, CSRF_PROTECTION_TOKEN, Lists.newArrayList("null", "12")), new VerboseExceptionMarshaller()); assertEquals(RpcResult.Status.SUCCESS, result.getStatus()); assertThat(result.getJsonResult(), containsString(TestService.GET_BY_ID_RESULT)); }
@Test public void causeException() { RpcResult result = call(new RpcCall(TestService.class.getName(), TestService.THROW_EXCEPTION_METHOD_NAME, CSRF_PROTECTION_TOKEN, new ArrayList<String>()), new VerboseExceptionMarshaller()); assertEquals(RpcResult.Status.EXCEPTION, result.getStatus()); assertThat(result.getJsonResult(), containsString(TestService.THROW_EXCEPTION_NAME)); }