Junit、Assert、内省、Properties类与配置文件的使用
来源:互联网 发布:sql商务系统开发培训 编辑:程序博客网 时间:2024/06/06 00:19
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 { @Testpublic 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+spublic 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文件的使用
0 0
- Junit、Assert、内省、Properties类与配置文件的使用
- JUnit加载properties配置文件
- junit.framework.Assert 与org.junit.Assert
- 配置文件properties的使用
- 配置文件的使用 Properties
- 配置文件(.properties)的使用
- properties配置文件的使用
- Properties类与配置文件
- Properties类与配置文件
- JUnit的assert方法
- log4j的使用与java中properties配置文件加载
- log4j的使用与java中properties配置文件加载
- properties配置文件的简单使用
- 初步了解Junit与assert
- 集合、泛型、IO流、多线程、Junit 、内省 、Properties、 路径问题
- junit测试类有自己的properties
- 黑马程序员-Properties类对配置文件的读取与操作。
- 使用Properties类读取项目外的配置文件
- codeforces101156L——Super 2048
- supervisor-进程管理工具
- 添加自定义alias命令
- 观察者模式
- CodeForces 585E
- Junit、Assert、内省、Properties类与配置文件的使用
- C/C++的中缀转后缀并求值的实现
- ViewPager 详解(三)---PagerTabStrip与PagerTitleStrip添加标题栏的异同
- Fast-RCNN代码解读(1)
- HDU 1053 Entropy【哈夫曼编码入门题】
- 一起学opencv(七) 边缘和角点检测
- 【留言板】有问题请在这里留言!
- 第三方框架RxJava和Retrofit结合及实例
- 66. Plus One