JAVA的Proxy动态代理在自动化测试中的应用

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<T> implements InvocationHandler{
	
	private Class<T> proxyMethod;

	public ProxyImp(Class<T> 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> T newInstance(Class<T> methodInterface) {
        final ProxyImp<T> proxyImp = new ProxyImp<T>(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.动态代理在自动化中的作用:

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

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

posted on 2016-02-03 17:29  张飞_  阅读(1782)  评论(3编辑  收藏  举报

导航