Junit 单元测试、BeanUtils、Properties类

一、  Junit单元测试

1.1. Junit单元测试框架的基本使用

 

一、搭建环境:

导入junit.jar包(junit4

 

二、写测试类:

0,一般一个类对应一个测试类。

1,测试类与被测试类最好是放到同一个包中(可以是不同的源文件夹)

2,测试类的名字为被测试类的名字加Test后缀。

 

三:写测试方法:

0,一般一个方法对应一个单元测试方法。

1,测试方法的名字为test前缀加被测试方法的名字,如testAddPerson()

2,单元测试方法上面要加上@Test注解(org.junit.Test)!

3,单元测试方法不能有参数,也不能有返回值(返回void)!测试的方法不能是静态的方法。

 

四、测试方法的基本使用:

1,可以单独执行一个测试方法,也可以一次执行所有的、一个包的、一个类中所有的测试方法。

2,执行完后,显示绿色表示测试成功;显示红色表示测试失败(抛异常后会测试失败)。

 

1.2. Assert断言工具类

 

其中有一些静态的工具方法(不符合期望就抛异常):

 

assertTrue(...) 参数的值应是true

assertFalse(...) 参数的值应是false  

 

assertNull(...) 应是null

assertNotNull(...) 应是非null的值

 

assertSame(...) 使用==比较的结果为true(表示同一个对象)

AssertNotSame(...) 使用==比较的结果为false

 

assertEquals(...) 两个对象equals()方法比较结果为true

 

1.3. 用于准备环境、清理环境的方法

@Test

表示单元测试方法。

 

@Before

所修饰的方法应是非static的(且没有参数,返回值为void)。

表示这个方法会在本类中的每个单元测试方法之前都执行一次。

 

@After

所修饰的方法应是非static的(且没有参数,返回值为void)。

表示这个方法会在本类中的每个单元测试方法之后都执行一次。

 

 

@BeforeClass

所修饰的方法应是static的(且没有参数,返回值为void)。

表示这个方法会在本类中的所有单元测试方法之前执行,只执行一次。

 

@AfterClass

所修饰的方法应是static的(且没有参数,返回值为void)。

表示这个方法会在本类中的所有单元测试方法之后执行,只执行一次。

 

 

二、 内省(Introspector)

为什么要学内省?

开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作java对象的属性。

 

内省是用于操作java对象的属性的,那么以下问题我们必须要清楚。

 

问题一: 什么是Java对象的属性和属性的读写方法?

 

问题二: 如何通过内省访问到javaBean的属性 ?

 

1. 通过PropertyDescriptor类操作Bean的属性.

 

public static void testPropertyDescriptor() throws Exception{

Person p = new Person();

PropertyDescriptor propertyDescriptor =  new PropertyDescriptor("id",Person.class);

//获取属性的写的方法。

Method writeMethod = propertyDescriptor.getWriteMethod();

Method readMethod = propertyDescriptor.getReadMethod();

propertyDescriptor.getReadMethod();

writeMethod.invoke(p, 12);

System.out.println(readMethod.invoke(p, null));

}

 

 

2. 通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后通过反射机制来调用这些方法。

 

public static void testIntrospector() throws Exception{

BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);

PropertyDescriptor[]  descriptor = beanInfo.getPropertyDescriptors();

for(PropertyDescriptor itemProperty : descriptor){

System.out.println(itemProperty.getReadMethod().getName());

}

}

 

存在的问题: sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils

 

public static void main(String[] args) throws Exception {

Person p = new Person();

ConvertUtils.register(new Converter() {

 

@Override

public Object convert(Class type, Object value) {

 try {

if(value!=null){

 

 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MM dd");

 Date d = dateFormat.parse((String) value);

 return d;

 }

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

return null;

}

}, Date.class);

 

BeanUtils.setProperty(p,"id","110");

BeanUtils.setProperty(p,"name","狗娃");

BeanUtils.setProperty(p, "birthDay","1992 12 12");

System.out.println(p.getId() +"=="+ p.getName()+"======"+p.getBirthDay());

}

 

三、 Properties类与配置文件

1.1 Properties配置文件说明

Properties类对应.properties文件。文件内容是键值对,键值对之间使用"="或空格隔开。开头是"#"的表示注释

 

Properties类在加载.properties文件时使用的iso8859-1的编码。所以这个文件中的中文要特殊处理:如果这个配置文件中有中文就必须要进行转义,使用native2ascii.exe命令操作:

native2ascii d:/my.properties d:/my2.properties

 

使用Properties类中的load(InputStream) 方法可以加载配置文件,使用其中的store(OutputStream) 方法可以保存配置到指定文件。

 

更多的信息可以看Properties类的API文档。

 

1.2 加载配置文件

 

1.3 写配置文件

1.4 使用Properties

public class DBUtil {

 

static Properties properties = new Properties();

 

static{

try {

Class clazz = DBUtil.class;

InputStreamReader fileReader =

new InputStreamReader(clazz.getResourceAsStream("/db.properties"));

properties.load(fileReader);

} catch (IOException e) {

e.printStackTrace();

}

}

public static String getUserName(){

String userName =properties.getProperty("userName");

return userName;

}

 

public static String getPassword(){

return properties.getProperty("password");

}

public static void main(String[] args) {

System.out.println("用户名:"+ getUserName());

System.out.println("密码: "+  getPassword());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值