java单元测试代码⽣成器
最近在学习jdk源码,打算把所有常⽤类的api熟悉⼀遍,通过编写单元测试的⽅式跑⼀遍,了解每⼀个api的⽤途,刚开始机构类是⼿写单测代码,但是效率很低,于是花了机构消失⾃⼰写了⼀个代码⽣成器,效率提⾼了很多,思想通⽤,下⾯附上代码
⾸先需要引⼊⼏个jar包(⾃⾏百度下载):
freemarker-2.3.27-incubating.jar
jdom2-2.0.6.jar
commons-lang3-3.7.jar
下⾯是项⽬结构:
下⾯是⽣成器代码:
package ator;
plate.Configuration;
plate.Template;
plate.TemplateException;
plate.TemplateExceptionHandler;
import org.apachemons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import flect.Constructor;
import flect.Method;
import flect.Modifier;
import java.util.*;
/**
/**
* 代码⽣成器
*
*/
public class CodeGenerator {
/**
* 包路径
*/
private static final String BASE_PACKAGE = "test";//项⽬中代码根路径
private String author;//注释中作者名称
private String sourceClass="";//源类全路径
private String templateName="";//模板名称
//项⽬根路径
private final String rootSrcPath = getClass().getResource("/").getPath().replace("/out/production/jdk-source/","/src/");
//模板路径
final String templateDir = rootSrcPath + "test/java/generator/template";
public CodeGenerator(String author, String sourceClass, String templateName) {
this.author = author;
this.sourceClass=sourceClass;
}
public void run() {
try {
//反射获取源class对象
Class sourceGalss=Class.forName(sourceClass);
//⽣成⽬标测试类
generate(sourceGalss, templateName);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws ClassNotFoundException {
//初始化代码⽣成器及执⾏
CodeGenerator codeGenerator=new CodeGenerator("zqw","urrent.atomic.AtomicIntegerFieldUpdater","test"); codeGenerator.run();
}
/**
* 使⽤freemarker模板⼯具⽣成⽬标代码
* void
* @throws
* @date 2020/6/17 22:28
*/
private void generate(Class sourceGalss, String templateName)
throws IOException, TemplateException {
//freemarker配置
Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
//设置freemarker模板路径
cfg.setDirectoryForTemplateLoading(new File(templateDir));
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
/
/获取模板对象
Template template = Template(templateName + ".ftl", "UTF-8");
// 定义模板数据
Package Package();
Map<String, Object> data = new HashMap<>();
//是否抽象类
if (Modifier.Modifiers())){
if (Modifier.Modifiers())){
data.put("isAbstract","1");
}else {
data.put("isAbstract","0");
}
//是否接⼝
if (Modifier.Modifiers())){
data.put("isInterface","1");
}else {
data.put("isInterface","0");
}
data.put("basePackage", BASE_PACKAGE+"."+Name());
data.put("realPackage", Name());
data.put("author", author);
Method[] DeclaredMethods();
List<String> methodNameList=new ArrayList<>();
for (int i = 0; i < methods.length; i++) {
Method methods1=methods[i];
if (Modifier.Modifiers())){
methodNameList.Name().substring(0,1).toUpperCase()+Name().substring(1));
}
}
Constructor[] DeclaredConstructors();
List<Integer> constructorList=new ArrayList<>();
int count=0;
for (int i = 0; i < constructors.length; i++) {
免费模板生成器
Constructor constructor=constructors[i];
if (Modifier.Modifiers())){
constructorList.add(count++);
}
}
data.put("methods",methodNameList);
data.put("constructorList",constructorList);
data.put("date", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
data.put("className", SimpleName());
// 输出⽂件
String fileName = SimpleName().substring(0, 1).toUpperCase() + SimpleName().substring(1); final String outFile =
rootSrcPath + place(".", "/") + "/" +Name().replace(".","/")+"/"+
fileName + "Test.java";
final String outFilePath=rootSrcPath + place(".", "/") + "/" +Name().replace(".","/");
File filePath=new File(outFilePath);
if (!ists()){
filePath.mkdirs();
}
Writer out = new FileWriter(outFile);
template.process(data, out);
System.out.println(templateName + "⽂件⽣成成功:" + outFile);
}
}
下⾯是模板⽂件代码:
package ${basePackage};
import ${realPackage}.${className};
import org.junit.Test;
/**
* ${className}的测试类
*
* @author ${author}
* @date ${date}
*/
public class ${className}Test {
<#if (isAbstract == "1")>
//abstract
</#if>
<#if (isInterface == "1")>
//interface
</#if>
<#if (isAbstract != "1")&&(isInterface != "1")>
<#list constructorList as count>
@Test
public void testConstruct${count}()throws Exception{ ${className} testObj=new ${className}();
}
</#list>
<#list methods as method>
@Test
public void test${method}(){
${className} testObj=new ${className}();
}
</#list>
</#if>
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论