Junit、Assert、内省、Properties类与配置文件的使用
2016-11-14 21:08
369 查看
Junit
Junit有什么用
可以不写main方法直接对方法进行测试怎么使用
1、导入junit.jar包2、加入@Test注释
3、例子如下
import org.junit.Test; public class Demo1 { @Test public void fun(){ System.out.println("Junit测试"); } }
使用规范
一个类如果需要测试,那么该类就应该对应着一个测试类,测试类的命名规范 : 被测试类的类名+ Test.一个被测试的方法一般对应着一个测试的方法,测试的方法的命名规范是: test+ 被测试的方法的方法名
例子如下:
//实际类 public class Tool public static int getMax(){ int a = 3; int b =5; int max = a>b?a:b; return max; } public static int getMin(){ int a = 3; int b = 5; int min = a<b?a:b; return min; } }
import org.junit.Test; //测试类 public class ToolTest { @Test public void testGetMax(){ int max = Tool.getMax(); if(max!=5){ throw new RuntimeException(); }else{ System.out.println("最大值:"+ max); } } @Test public void testGetMin(){ int min = Tool.getMin(); if(min!=3){ throw new RuntimeException(); }else{ System.out.println("最小值:"+ min); } } }
使用注意点
如果使用junit测试一个方法的时候,在junit窗口上显示绿条那么代表测试正确,如果是出现了红条,则代表该方法测试出现了异常不通过。如果点击方法名、 类名、包名、 工程名运行junit分别测试的是对应的方法,类、 包中 的所有类的test方法,工程中的所有test方法。
@Test测试的方法不能是static修饰与不能带有形参。
如果测试一个方法的时候需要准备测试的环境或者是清理测试的环境,那么可以@Before、 @After 、@BeforeClass、 @AfterClass这四个注解。
@Before、 @After 是在每个测试方法测试的时候都会调用一次,
@BeforeClass、 @AfterClass是在所有的测试方法测试之前与测试之后调用一次而已。
案例如下:
import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class Demo2 { //@Before @BeforeClass public static void before(){ System.out.println("方法调用前..."); } @Test public void fun1() { System.out.println("我是方法一"); } @Test public void fun2(){ System.out.println("我是方法二"); } //@After @AfterClass public static void after(){ System.out.println("方法调用后.."); } }
运行如下:
方法调用前... 我是方法二 我是方法一 方法调用后..
使用@Before和@After同时去掉static效果如下:
方法调用前... 我是方法二 方法调用后.. 方法调用前... 我是方法一 方法调用后..
Assert
Assert有什么用
可以直接使用Assert提供的方法来判断对错,由Junit运行红蓝来判断Assert有以下几种方法
import junit.framework.Assert; import org.junit.Test; //测试类 public class ToolTest { @Test public void testGetMax(){ Assert.assertSame(5,5); //Assert.assertSame(5, max); // expected 期望 actual 真实 //Assert.assertSame(new String("abc"), "abc"); //Assert.assertEquals(new String("abc"), "abc"); //底层是使用Equals方法比较的 //Assert.assertNull("aa"); //Assert.assertTrue(true); }
内省
JavaBean
JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且方法名符合一定规则。一个JavaBean需要满足以下条件:
1)必须有无参构造函数
2)属性必须私有, 我们称为字段
3)提供标准的getter和setter
//快捷键是shift+alt+s public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override //为了方便打印,添加一个toString方法 public String toString() { return "User [age=" + age + ", name=" + name + "]"; } }
内省是什么
通过反射的方式访问javabean的技术内省有什么用
可以实现一种通用性1、传统的方式访问JavaBean
public class Demo1 { public static void main(String[] args) { User user=new User(); user.setName("zhangsan"); user.setAge(19); System.out.println(user); } }
2、使用内省的方式来访问javabean
PropertyDescriptor类操作Bean的属性
public class Demo1 { public static void main(String[] args) throws Exception { User user=new User(); //创建属性描述器 PropertyDescriptor descriptor=new PropertyDescriptor("name",User.class); //获得写方法 Method writeMethod=descriptor.getWriteMethod(); //调用写方法 writeMethod.invoke(user, "lisi"); System.out.println(user); } }
3.简化书写,实现通用性。
import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import cn.itcast.day08.domain.User; public class Demo2 { /** * @param args * @throws IntrospectionException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public static void main(String[] args) throws Exception { // 内省 User user = new User(); setProperty(user, "name", "wangwu"); setProperty(user, "age", 11);//若为"11"的话就会出现类型错误 System.out.println(user); } // 实现一个通用的方法 为任意的一个javabean的任意属性赋任意值 public static void setProperty(Object bean, String fieldName, Object value) throws Exception { // 创建属性描述器 PropertyDescriptor descriptor = new PropertyDescriptor(fieldName, bean.getClass()); // 获得 写方法 Method writeMethod = descriptor.getWriteMethod(); // 调用 写方法 writeMethod.invoke(bean, value); } }
BeanUtils
由于内省用起来特别麻烦,Apache组织开发了一套用于操作JavaBean的API BeanUtils,如下详讲:(1)可以支持String到8中基本数据类型转换
(2)其他引用数据类型都需要注册转换器 ConvertUtils.register(Converter, Class)
如何使用
需导入两个jar包:commons-beanutils-1.8.0.jar、commons-logging.jar案例如下 :
import java.util.Date; public class User { private int id; private String name; private double salary; private Date birthday; public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public User(int id, String name, double salary) { super(); this.id = id; this.name = name; this.salary = salary; } public User(){} @Override public String toString() { return "编号:"+this.id+" 姓名:"+ this.name+ " 薪水:"+ this.salary+" 生日:"+ birthday; } }
import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; /* BeanUtils: BeanUtils主要解决 的问题: 把对象的属性数据封装 到对象中。 BeanUtils的好处: 1. BeanUtils设置属性值的时候,如果属性是基本数据 类型,BeanUtils会自动帮我转换数据类型。 2. BeanUtils设置属性值的时候底层也是依赖于get或者Set方法设置以及获取属性值的。 3. BeanUtils设置属性值,如果设置的属性是其他的引用 类型数据,那么这时候必须要注册一个类型转换器。 BeanUtilss使用的步骤: 1. 导包commons-logging.jar 、 commons-beanutils-1.8.0.jar */ public class Demo3 { public static void main(String[] args) throws Exception { //从文件中读取到的数据都是字符串的数据,或者是表单提交的数据获取到的时候也是字符串的数据。 String id ="110"; String name="陈其"; String salary = "1000.0"; String birthday = "2013-12-10"; //使用匿名内部内注册一个类型转换器 ConvertUtils.register(new Converter() { @Override public Object convert(Class type, Object value) { // type : 目前所遇到的数据类型。 value :目前参数的值。 Date date = null; try{ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); date = dateFormat.parse((String)value); }catch(Exception e){ e.printStackTrace(); } return date; } }, Date.class); User e = new User(); BeanUtils.setProperty(e, "id", id); BeanUtils.setProperty(e, "name",name); BeanUtils.setProperty(e, "salary",salary); BeanUtils.setProperty(e, "birthday",birthday); System.out.println(e); } }
Properties类与配置文件
java.util.Properties类和.properties文件的使用相关文章推荐
- Java读写配置文件——Properties类的简要使用笔记
- 使用Properties类读取项目外的配置文件
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
- 使用java.util.Properties类读写配置文件
- java使用Properties类生成配置文件
- java使用Properties类读取配置文件信息
- 使用Properties类加载配置文件
- 使用java.util.Properties类读写配置文件
- 【深入JAVA】Java中使用配置文件——properties类
- Java使用Properties类读取配置文件
- 如何使用.NET配置文件(转载)
- 如何使用.NET配置文件(一)
- 如何使用.NET配置文件
- 属性配置文件的使用方法
- 如何使用XCodeFactory自动生成XML配置文件和对应的解析类?
- 使用NUnit进行单元测试,读配置文件的问题
- Nhibernate拒绝配置文件(NHibernate.Mapping.Attributes的使用)
- [原创]JAAS 实现in Struts Web App,使用XMLPolicy文件,不改变VM安全配置(1)认证
- 使用.NET Remoting开发分布式应用——配置文件篇
- 结合 FileSystemWatcher更好的使用配置文件