之前有基于 testNG+okHttp 封装了一个接口自动化测试的执行器,将测试用例都放在 json 文件中: [接口测试平台一期] 接口测试用例参数化方案
但是一直都是按顺序执行的,现在想要实现同时执行多个用例,但是因为我的执行器就一个类文件,
如果想要同时执行,基于 testNG 的话,需要多个类,那么问题来了,如何自动生成多个类文件呢?
这两者都支持继承实现动态代理,
cglib 在继承注解时,存在一定的问题,自定义注解中未加入@Inherited, 是不会继承该注解的,或者是我没找到解决的方法
javassist 更强大,字节码级别的,可在运行时修改方法,注解参数等
两个坑都踩了,最后还是用 javassist 实现了
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.24.1-GA</version>
</dependency>
CtClass cc = pool.makeClass("TestExecutor" + caseSetIndex);
CtClass superClass = pool.getCtClass("com.wz.exec.BaseExecutor");
cc.setSuperclass(superClass);
CtMethod method = CtNewMethod.make("public void executor() { run();}", cc);
ConstPool methodPool = method.getMethodInfo().getConstPool();
AnnotationsAttribute attribute = new AnnotationsAttribute(methodPool, AnnotationsAttribute.visibleTag);
Annotation annotation = new Annotation("org.testng.annotations.Test", methodPool);
annotation.addMemberValue("invocationCount", new IntegerMemberValue(methodPool, 1));
annotation.addMemberValue("description", new StringMemberValue(apiTestCase.getCaseInfo().getCaseName(), methodPool));
attribute.addAnnotation(annotation);
method.getMethodInfo().addAttribute(attribute);
cc.addMethod(method);
Class executor = cc.toClass();
# 实际这里我塞进去的是一个列表
testNG.setTestClasses(executor);
因为最近在看《Java 程序性能优化 让你的 Java 程序更快、更稳定》,接触到 javassist,
但在百度查的时候发现,这个套路在 12 年的时候就有人玩过
还是自己太无知了
因为是动态生成的,如果生成了很多的类,会存在内存耗尽的问题,
所以可以更进一步,思考内存消耗的问题,
可以自定义 classloader 来加载这些动态生成的类,用完就释放
Book:《Java 程序性能优化 让你的 Java 程序更快、更稳定》
javassist 官网:http://www.javassist.org/tutorial/tutorial2.html#add