ITPub博客

首页 > 数据库 > 数据库开发技术 > java测试框架如何执行groovy脚本文件

java测试框架如何执行groovy脚本文件

原创 数据库开发技术 作者:FunTester 时间:2019-10-24 10:43:55 0 删除 编辑

本人在写基于httpclient的测试框架时,用到了groovy脚本作为测试用例的脚本语言,自然就需要java执行上传的测试脚本,在看过实例之后,自己进行了封装,总体来说跟java反射执行java方法类似。但又有一些不兼容的情况,部分已经写了博客做记录了,以后会陆续更新。分享代码,供大家参考。

其中一个比较大的区别时,在获取groovy类加载器的时候必须是非静态的。

  1package com.fission.source.source;
2
3import groovy.lang.GroovyClassLoader;
4import groovy.lang.GroovyObject;
5import org.codehaus.groovy.tools.GroovyClass;
6import java.io.File;
7import java.io.IOException;
8import java.lang.reflect.Method;
9import java.util.ArrayList;
10import java.util.List;
11
12public class ExcuteGroovy extends SourceCode {
13    private String path;
14    private String name;
15    private String[] args;//参数,暂时不支持参数
16    private List<String> files = new ArrayList<>();//所有脚本
17    private GroovyClassLoader loader = new GroovyClassLoader(getClass().getClassLoader());
18    private GroovyObject groovyObject;//groovy对象
19    private Class<?> groovyClass;//执行类
20
21
22    public ExcuteGroovy(String path, String name) {
23        this.path = path;
24        this.name = name;
25        getGroovyObject();
26    }
27
28    /**
29     * 执行一个类的所有方法
30     */

31    public void excuteAllMethod(String path) {
32        getAllFile(path);
33        if (files == null)
34            return;
35        files.forEach((file) -> new ExcuteGroovy(file, "").excuteMethodByPath());
36    }
37
38    /**
39     * 执行某个类的方法,需要做过滤
40     *
41     * @return
42     */

43    public void excuteMethodByName() {
44        if (new File(path).isDirectory()) {
45            output("文件类型错误!");
46        }
47        try {
48            groovyObject.invokeMethod(name, null);
49        } catch (Exception e) {
50            output("执行" + name + "失败!", e);
51        }
52
53    }
54
55    /**
56     * 根据path执行相关方法
57     */

58    public void excuteMethodByPath() {
59        Method[] methods = groovyClass.getDeclaredMethods();//获取类方法,此处方法比较多,需过滤
60        for (Method method : methods) {
61            String methodName = method.getName();
62            if (methodName.contains("test") || methodName.equals("main")) {
63                groovyObject.invokeMethod(methodName, null);
64            }
65        }
66    }
67
68    /**
69     * 获取groovy对象和执行类
70     */

71    public void getGroovyObject() {
72        try {
73            groovyClass = loader.parseClass(new File(path));//创建类
74        } catch (IOException e) {
75            output(e);
76        }
77        try {
78            groovyObject = (GroovyObject) groovyClass.newInstance();//创建类对象
79        } catch (InstantiationException e) {
80            output(e);
81        } catch (IllegalAccessException e) {
82            output(e);
83        }
84    }
85
86    /**
87     * 获取文件下所有的groovy脚本,不支持递归查询
88     *
89     * @return
90     */

91    public List<String> getAllFile(String path) {
92        File file = new File(path);
93        if (file.isFile()) {
94            files.add(path);
95            return files;
96        }
97        File[] files1 = file.listFiles();
98        int size = files1.length;
99        for (int i = 0; i < size; i++) {
100            String name = files1[i].getAbsolutePath();
101            if (name.endsWith(".groovy"))
102                files.add(name);
103        }
104        return files;
105    }
106}

在获取groovy脚本的时候,并未用到递归,以后随着需求增加应该会增加递归。

技术类文章精选

非技术文章精选

大咖风采


原文作者:FunTester;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2019-07-07

  • 博文量
    1037
  • 访问量
    420711