### Question:
DynamicProxy implements InvocationHandler { public static Object getProxy(Object realSubject) { InvocationHandler handler = new DynamicProxy(realSubject); return Proxy.newProxyInstance(handler.getClass().getClassLoader(), realSubject.getClass().getInterfaces(), handler); } DynamicProxy(Object subject); @Override Object invoke(Object proxy, Method method, Object[] args); static Object getProxy(Object realSubject);  }### Answer:
@Test public void resourceServiceTest() { ResourceServiceImpl realService = new ResourceServiceImpl(); ResourceServiceImpl proxyService = (ResourceServiceImpl) DynamicProxy.getProxy(realService); proxyService.checkResource(new User()); }
@Test public void failUserServiceTest() { UserService realService = new UserServiceImpl(); UserService proxyService = (UserService) DynamicProxy.getProxy(realService); User user = proxyService.queryUser(new QueryUserCondition()); LOGGER.info("failUserServiceTest: " + user); }
@Test public void successUserServiceTest() { UserService realService = new UserServiceImpl(new AlwaysSuccessOutServiceImpl()); UserService proxyService = (UserService) DynamicProxy.getProxy(realService); User user = proxyService.queryUser(new QueryUserCondition()); LOGGER.info("successUserServiceTest: " + user); }
@Test public void randomUserServiceTest() { UserService realService = new UserServiceImpl(new RandomOutServiceImpl(50)); UserService proxyService = (UserService) DynamicProxy.getProxy(realService); User user = proxyService.queryUser(new QueryUserCondition()); LOGGER.info("randomUserServiceTest: " + user); }
@Test public void roleServiceTest() { RoleService realService = new RoleServiceImpl(); RoleService proxyService = (RoleService) DynamicProxy.getProxy(realService); boolean hasPrivilege = proxyService.hasPrivilege(new User()); LOGGER.info("roleServiceTest: " + hasPrivilege); }