博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA的Proxy动态代理在自动化测试中的应用
阅读量:7286 次
发布时间:2019-06-30

本文共 2038 字,大约阅读时间需要 6 分钟。

JAVA的动态代理,在MYBATIS中应用的很广,其核心就是写一个interface,但不写实现类,然后用动态代理来实例化并执行这个interface中的方法,话不多说,来看一个实现的例子:

1.先定义一个接口:

public interface TestProxy {		String hello();	}

 2.虽然不写实现类,但我们仍然希望在执行这个hello()方法时,能输出我们想要输出的内容,比如我把希望要输出的内容放在一个属性文件中:

hello=world

 我希望在调用hello方法时,输出world,接下来得解析这个属性文件:

public class PropertiesHandler {			private static Properties p = new Properties();		static{		try {			InputStream in = new FileInputStream(new File("src/test.properties"));			p.load(in);			in.close();		} catch (IOException e) {			throw new RuntimeException("test.properties load error!");		}	}		public static String getProperty(String key){		try{			return p.getProperty(key, null);		}catch(Exception e){			e.printStackTrace();		}		return "";	}	}

 3.解析完后,我们再写一个动态代理类:

public class ProxyImp
implements InvocationHandler{ private Class
proxyMethod; public ProxyImp(Class
proxyMethod) { this.proxyMethod = proxyMethod; } @Override public Object invoke(Object proxy, Method method, Object[] args)throws Throwable { String value = PropertiesHandler.getProperty(method.getName()); System.out.println(value); return null; }}

 其原理就是在调用接口类方法时,动态代理类都会去执行这个invoke方法,以达到执行的目的,可以看到,在invoke方法里,我们把hello方法在属性文件中对应的值给取出来了,并输出。

4.接下来就是再封装一个代理工厂类来产生这个接口的一个实例:

public class ProxyImpFactory{		@SuppressWarnings("unchecked")	public static 
T newInstance(Class
methodInterface) { final ProxyImp
proxyImp = new ProxyImp
(methodInterface); return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{methodInterface}, proxyImp); } }

 可以从上面的代码中看出这个代理类会自动的生成一个接口实现类的实例。

5.接下来就是真正的调用了:

public static void main(String[] args) {		TestProxy tp = ProxyImpFactory.newInstance(TestProxy.class);		tp.hello();	}

 输出就是:world.

6.动态代理在自动化中的作用:

 自动化中,当把元素对象用外部文件,把数据文件用外部文件时,都可以用这种方式来进行封装,其在自动化测试中最大的作用,我想就是利用在封装关键字上了,把关键字与具体的方法对应起来,就可以用这样的方式。

 至于具体的关键字的实现,我想得靠大家自已了,有兴趣的可以与我讨论!

转载地址:http://eppjm.baihongyu.com/

你可能感兴趣的文章
备份架构——三种基本备份拓扑
查看>>
关于visual assist x插件不能用的解决方案
查看>>
Linux iptables:规则组成
查看>>
HDU 4442 Physical Examination【水题】【思维题】
查看>>
NET 命令 常用方法!
查看>>
我的友情链接
查看>>
memcached
查看>>
谁搞死了你的软件?
查看>>
Promise 对象
查看>>
Windows快速添加IP地址
查看>>
AS3.0 事件流
查看>>
“将截断字符串或二进制数据。语句已终止……”问题的解决
查看>>
红苹果IP代理软件 v6.2
查看>>
Centos5.x & Centos6.x 使用mail命令发邮件以及如何伪造发件人
查看>>
JavaScript系列:ECMAScript原始类型
查看>>
centos反编译APK包
查看>>
CSS系列:CSS中盒子的浮动与定位
查看>>
windows 用户用户组迁移
查看>>
Linux系统扩充2
查看>>
linux新手的心得
查看>>