一、Junit
1.Junit主要用于做局部测试,局部测试 局部测试可以快速帮助查找bug
2.测试分类:黑盒测试与白盒测试
3.黑盒测试:功能测试(按照需求的文档来进行测试)与接口测试 比较简单
4.白盒测试:测开编写自动化的脚本
5.Junit使用步骤
定义一个方法 (方法修饰符public 返回值只能使用void)
在方法上加上注解@Test
jar下载地址:https://mvnrepository.com/
新建一个lib文件
将jar放到lib目录下
将jar添加项目依赖
二、反射的概念
1.所有的框架都使用了反射技术
java中所有类在反射面前都是裸体(可以通过反射获取到所有的资源,包括私有的)
2.反射机制:将类的各个部分组成一个新的对象Class对象
三、获取Class对象
1、通过调用类的getClass方法进行获取,对象.getClass()
2.通过当前类名.class来获取
3.通过Class.forName(完整的包名+类名) 最常用,
四、获取构造方法
五、获取成员方法
六、获取成员变量
七、案例
不改变java代码任意获取类中的方法
通过配置文件将需要的数据传输进去(properties文件键值对)
代码-需要获取方法的类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.getmethod; public class Student { private String name; private int pws; public Student() { } public static void show(String name,int pwd){ System.out.println(name); System.out.println(pwd); } }
配置文件 admin.properties
代码-配置文件工具类
复制代码
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
32package com.getmethod; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class Properutils { private static Properutils properutils; private static Properties properties; private Properutils(){ properties=new Properties(); InputStream resourceAsStream =Properutils.class.getResourceAsStream("admin.properties"); try { properties.load(resourceAsStream); resourceAsStream.close(); } catch (IOException e) { e.printStackTrace(); } } public static synchronized Properutils getIntance(){ if (properutils==null){ properutils=new Properutils(); } return properutils; } public static String getValue(String key){ return properties.getProperty(key); } }
代码-测试类
复制代码
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@Test public void show2() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { String className = Properutils.getIntance().getValue("ClassName"); String methodName = Properutils.getIntance().getValue("MethodName"); String parm = Properutils.getIntance().getValue("parm"); String typeName = Properutils.getIntance().getValue("TypeName"); Class<?> aClass = Class.forName(className); Object o = aClass.newInstance(); Constructor<?> constructor = aClass.getConstructor(); Object o1 = constructor.newInstance(); Class[] getclass = getclass(parm); Object[] getty = getobj(parm, typeName); if (getclass == null && getclass.length <= 0) { Method declaredMethod = aClass.getDeclaredMethod(methodName); declaredMethod.invoke(o); } else { Method declaredMethod = aClass.getDeclaredMethod(methodName, getclass); declaredMethod.invoke(o, getty); } } public static Class[] getclass(String parm){ if (parm==null||"".equals(parm)){ return null; }else { List<Class> li=new ArrayList<>(); String[] split = parm.split(","); for (String s:split){ if (s.equals("String")){ li.add(String.class); }else if (s.equals("int")){ li.add(int.class); }else{ li.add(Object.class); } } Class[] classes = li.toArray(new Class[li.size()]); return classes; } } public static Object[] getobj(String parm,String typeName){ if (parm==null||"".equals(parm)||typeName==null||"".equals(typeName)){ return null; }else{ List<Object> li=new ArrayList<>(); String[] split = parm.split(","); String[] split1 = typeName.split(","); for (int i=0;i<split.length;i++){ if (split[i].equals("String")){ li.add(split1[i]); }else if (split[i].equals("int")){ li.add(Integer.parseInt(split1[i])); }else { li.add((Object)split1[i]); } } Object[] objects = li.toArray(new Object[li.size()]); return objects; } }
最后
以上就是个性纸鹤最近收集整理的关于25.反射、Junit、获取Class对象、获取构造方法、获取成员方法、获取成员变量、案例的全部内容,更多相关25内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复