您的位置:首页 > 编程语言 > Java开发

如何遍历执行一个包里面每个类的用例方法

2018-01-31 09:16 531 查看
本人在使用 httpclient 做接口测试的过程中,用例是以代码形式写在一个用例包里面的,包里的每个类表示的一类用例,大致是按照接口所在模块划分。这样就导致了一个问题,执行用例必须得把用例包里面所以类的用例方法都执行一边。之前使用过java 的反射来根据类名创建类对象,然后根据方法名执行相应的方法。根据这个思路,加之上网查找了一些相关资料参考了一些其他人的代码,自己封装了一个执行用例包里面所有类的用例方法的用例执行类,分享出来,供大家参考。

package source;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class excuteSource extends SourceCode {

public static void main(String[] args) {
excuteAllMethodInPackage("pie.normal");
}

/**
* 执行包内所有类的非 main 方法
*
* @param packageName
*/
public static void excuteAllMethodInPackage(String packageName) {
// String packageName = "najm.base";
List<String> classNames = getClassName(packageName);
if (classNames != null) {
for (String className : classNames) {
String path = packageName + "." + className;
excuteAllMethod(path);// 执行所有方法
}
}
}

/**
* 获取实例对象的所有 public 方法
*
* @param object
* @return
*/
public static Method[] getAllMethod(Object object) {
Class<?> class1 = object.getClass();
class1.getName();
Method[] methods = class1.getDeclaredMethods();
// Method[] methods = class1.getMethods();//此处获取的所有方法,包括继承来的
return methods;
}

/**
* 获取实例对象所有 public 方法,并且执行
*
* @param object
*/
public static void excuteAllMethod(Object object) {
Class<?> class1 = object.getClass();
Method[] methods = class1.getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("main")) {
continue;
}
executeMethodByName(method.getName(), class1.getName());
}
}

/**
* 执行一个类的方法内所有的方法,非 main
*
* @param path
* 类名
*/
public static void excuteAllMethod(String path) {
Class<?> c = null;
Object object = null;
try {
c = Class.forName(path);
object = c.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
Class<?> class1 = object.getClass();
Method[] methods = class1.getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("main")) {// 排除 mian 方法
continue;
}
executeMethodByName(method.getName(), class1.getName());
}
}

/**
* 根据方法名执行相应方法,利用反射,适用于带参方法
*
* @param api_name
* 接口名称
* @param use
* 传入参数,此处为适配新的请求方法
* @return 返回请求json数据
*/
public static void executeMethodByName(String mehtod, String path) {
Object obj = null;
Method method = null;
String className = null;
try {
// 里面写自己的类名及路径
Class<?> c = Class.forName(path);
obj = c.newInstance();
className = c.getCanonicalName();
// 第一个参数写的是方法名,第二个\第三个\...写的是方法参数列表中参数的类型
method = c.getMethod(mehtod);
// invoke是执行该方法,并携带参数值
} catch (Exception e) {
output("反射执行出错!", e);
}
try {
output("执行" + className + "类的" + method.getName() + "方法");
method.invoke(obj);
} catch (Exception e) {
output("反射运行方法异常!", e);
}
}

/**
* 获取某包下所有类
*
* @param packageName
* 包名
* @param childPackage
* 是否遍历子包
* @return 类的完整名称
*/
public static List<String> getClassName(String packageName) {
List<String> fileNames = new ArrayList<>();
ClassLoader loader = Thread.currentThread().getContextClassLoader();// 获取当前位置
String packagePath = packageName.replace(".", "/");// 转化路径,Linux 系统
URL url = loader.getResource(packagePath);// 具体路径
if (url == null || !"file".equals(url.getProtocol())) {
output("获取类名失败!");
return fileNames;
}
File file = new File(url.getPath());
File[] childFiles = file.listFiles();
for (File childFile : childFiles) {
String path = childFile.getPath();
if (path.endsWith(".class")) {
path = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
fileNames.add(path);
}
}
return fileNames;
}
}main 方法里面写的就是使用方法,这里需要提醒一点,一定要对方法名进行过滤,不然可能会把其他类的 main 方法也执行了。
末了,宣传一下自己的 QQ群:群号:340964272

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐