【Java】实体类单元测试覆盖率提升工具

提升sonar代码覆盖率,实体类单元测试覆盖率提升工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
* @author xinchen 2017年9月28日
* @version 1.0
* @description: 实体类单元测试覆盖率提升工具
*/
public class EntityVoTestUtils {

//实体化数据
private static final Map<String, Object> STATIC_MAP = new HashMap<String, Object>();

//忽略的函数方法method
private static final String NO_NOTICE = "getClass,notify,notifyAll,wait,equals,hashCode,clone";

// private static final List<Class> CLASS_LIST = new ArrayList<Class>();

static {
STATIC_MAP.put("java.lang.Long", 1L);
STATIC_MAP.put("java.lang.String", "test");
STATIC_MAP.put("java.lang.Integer", 1);
STATIC_MAP.put("int", 1);
STATIC_MAP.put("long", 1);
STATIC_MAP.put("java.util.Date", new Date());
STATIC_MAP.put("char", '1');
STATIC_MAP.put("java.util.Map", new HashMap());
STATIC_MAP.put("boolean", true);

// CLASS_LIST.add(.class);
// CLASS_LIST.add(.class);
}


/**
* @param CLASS_LIST 类列表
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws InstantiationException
*/
public static void justRun(List<Class> CLASS_LIST)
throws IllegalAccessException, InvocationTargetException, InstantiationException {
for (Class temp : CLASS_LIST) {
Object tempInstance = new Object();
//执行构造函数
Constructor[] constructors = temp.getConstructors();
for (Constructor constructor : constructors) {
final Class<?>[] parameterTypes = constructor.getParameterTypes();
if (parameterTypes.length == 0) {
tempInstance = constructor.newInstance();
} else {
Object[] objects = new Object[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
objects[i] = STATIC_MAP.get(parameterTypes[i].getName());
}
tempInstance = constructor.newInstance(objects);
}
}

//执行函数方法
Method[] methods = temp.getMethods();
for (final Method method : methods) {
if (NO_NOTICE.contains(method.getName())) {
break;
}
final Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length != 0) {
Object[] objects = new Object[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
objects[i] = STATIC_MAP.get(parameterTypes[i].getName());
}
method.invoke(tempInstance, objects);
} else {
method.invoke(tempInstance);
}
}
//输出执行完的类名
// System.out.println(temp.getName());
}
}
}