本人在写基于httpclient的测试框架时,用到了groovy脚本作为测试用例的脚本语言,自然就需要java执行上传的测试脚本,在看过实例之后,自己进行了封装,总体来说跟java反射执行java方法类似。但又有一些不兼容的情况,部分已经写了博客做记录了,以后会陆续更新。分享代码,供大家参考。
其中一个比较大的区别时,在获取groovy类加载器的时候必须是非静态的。
package com.fission.source.source; import groovy.lang.GroovyClassLoader; import groovy.lang.GroovyObject; import org.codehaus.groovy.tools.GroovyClass; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class ExcuteGroovy extends SourceCode { private String path; private String name; private String[] args;//参数,暂时不支持参数 private List<String> files = new ArrayList<>();//所有脚本 private GroovyClassLoader loader = new GroovyClassLoader(getClass().getClassLoader()); private GroovyObject groovyObject;//groovy对象 private Class<?> groovyClass;//执行类 public ExcuteGroovy(String path, String name) { this.path = path; this.name = name; getGroovyObject(); } /** * 执行一个类的所有方法 */ public void excuteAllMethod(String path) { getAllFile(path); if (files == null) return; files.forEach((file) -> new ExcuteGroovy(file, "").excuteMethodByPath()); } /** * 执行某个类的方法,需要做过滤 * * @return */ public void excuteMethodByName() { if (new File(path).isDirectory()) { output("文件类型错误!"); } try { groovyObject.invokeMethod(name, null); } catch (Exception e) { output("执行" + name + "失败!", e); } } /** * 根据path执行相关方法 */ public void excuteMethodByPath() { Method[] methods = groovyClass.getDeclaredMethods();//获取类方法,此处方法比较多,需过滤 for (Method method : methods) { String methodName = method.getName(); if (methodName.contains("test") || methodName.equals("main")) { groovyObject.invokeMethod(methodName, null); } } } /** * 获取groovy对象和执行类 */ public void getGroovyObject() { try { groovyClass = loader.parseClass(new File(path));//创建类 } catch (IOException e) { output(e); } try { groovyObject = (GroovyObject) groovyClass.newInstance();//创建类对象 } catch (InstantiationException e) { output(e); } catch (IllegalAccessException e) { output(e); } } /** * 获取文件下所有的groovy脚本,不支持递归查询 * * @return */ public List<String> getAllFile(String path) { File file = new File(path); if (file.isFile()) { files.add(path); return files; } File[] files1 = file.listFiles(); int size = files1.length; for (int i = 0; i < size; i++) { String name = files1[i].getAbsolutePath(); if (name.endsWith(".groovy")) files.add(name); } return files; } }
在获取groovy脚本的时候,并未用到递归,以后随着需求增加应该会增加递归。
有兴趣的同学一起加群交流:群号:340964272