文章目录
- ∆ Junit单元测试
- 1.1. Junit单元测试框架的基本使用
- 1.2. Assert断言工具类
- 1.3. 用于准备环境、清理环境的方法
- ∆ BeanUtils(主要解决的问题:把对象的属性数据封装 到对象中)
- ∆ 文件路径
- 1.1. 绝对路径
- 1.2. 相对路径
- 1.3. Java程序中的相对路径
- 1.4. classpath路径
∆ Junit单元测试
1.1. Junit单元测试框架的基本使用
一、搭建环境:
导入junit.jar包(junit4)
二、写测试类:
0,一般一个类对应一个测试类。
1,测试类与被测试类最好是放到同一个包中(可以是不同的源文件夹)
2,测试类的名字为被测试类的名字加Test后缀。
三:写测试方法:
0,一般一个方法对应一个单元测试方法。
1,测试方法的名字为test前缀加被测试方法的名字,如testAddPerson()。
2,单元测试方法上面要加上@Test注解(org.junit.Test)!
3,单元测试方法不能有参数,也不能有返回值(返回void)!测试的方法不能是静态的方法。
四、测试方法的基本使用:
1,可以单独执行一个测试方法,也可以一次执行所有的、一个包的、一个类中所有的测试方法。
2,执行完后,显示绿色表示测试成功;显示红色表示测试失败(抛异常后会测试失败)。
五、代码展示:
public class Demo1 {
@Test //注解
public void getMax(int a, int b){
/* int a = 3;
int b = 5 ;*/
int max = a>b?a:b;
System.out.println("最大值:"+max);
}
@Test
public void sort(){
int[] arr = {12,4,1,19};
for(int i = 0 ; i < arr.length-1 ; i++){
for(int j = i+1 ; j<arr.length ; j++){
if(arr[i]>arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("数组的元素:"+Arrays.toString(arr));
}
}
1.2. Assert断言工具类
一、其中有一些静态的工具方法(不符合期望就抛异常):
assertTrue(...) 参数的值应是true
assertFalse(...) 参数的值应是false
assertNull(...) 应是null值
assertNotNull(...) 应是非null的值
assertSame(...) 使用==比较的结果为true(表示同一个对象)
AssertNotSame(...) 使用==比较的结果为false
assertEquals(...) 两个对象equals()方法比较结果为true
assertEquals(...) 两个对象equals()方法比较结果为true
二、代码展示:
//测试类
public class ToolTest {
@Test
public void testGetMax(){
int max = Tool.getMax();
if(max!=5){
throw new RuntimeException();
}else{
System.out.println("最大值:"+ max);
}
//断言
//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);
}
@Test
public void testGetMin(){
int min = Tool.getMin();
if(min!=3){
throw new RuntimeException();
}else{
System.out.println("最小值:"+ min);
}
}
}
1.3. 用于准备环境、清理环境的方法
@Test
表示单元测试方法。
@Before
所修饰的方法应是非static的(且没有参数,返回值为void)。
表示这个方法会在本类中的每个单元测试方法之前都执行一次。
@After
所修饰的方法应是非static的(且没有参数,返回值为void)。
表示这个方法会在本类中的每个单元测试方法之后都执行一次。
@BeforeClass
所修饰的方法应是static的(且没有参数,返回值为void)。
表示这个方法会在本类中的所有单元测试方法之前执行,只执行一次。
@AfterClass
所修饰的方法应是static的(且没有参数,返回值为void)。
表示这个方法会在本类中的所有单元测试方法之后执行,只执行一次。
代码展示:
public class Demo2 {
//准备测试的环境
//@Before
@BeforeClass
public static void beforeRead(){
System.out.println("准备测试环境成功...");
}
//读取文件数据,把把文件数据都
@Test
public void readFile() throws IOException{
FileInputStream fileInputStream = new FileInputStream("F:\\a.txt");
int content = fileInputStream.read();
System.out.println("内容:"+content);
}
@Test
public void sort(){
System.out.println("读取文件数据排序..");
}
//清理测试环境的方法
// @After
@AfterClass
public static void afterRead(){
System.out.println("清理测试环境..");
}
}
∆ BeanUtils(主要解决的问题:把对象的属性数据封装 到对象中)
BeanUtils的好处
1. BeanUtils设置属性值的时候,如果属性是基本数据 类型,BeanUtils会自动帮我转换数据类型。
2. BeanUtils设置属性值的时候底层也是依赖于get或者Set方法设置以及获取属性值的。
3. BeanUtils设置属性值,如果设置的属性是其他的引用 类型数据,那么这时候必须要注册一个类型转换器。
BeanUtilss使用的步骤:
- 导包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);
Emp e = new Emp();
BeanUtils.setProperty(e, "id", id);
BeanUtils.setProperty(e, "name",name);
BeanUtils.setProperty(e, "salary",salary);
BeanUtils.setProperty(e, "birthday",birthday);
System.out.println(e);
}
}
Emp.java
import java.util.Date;
public class Emp {
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 Emp(int id, String name, double salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
public Emp(){}
@Override
public String toString() {
return "编号:"+this.id+" 姓名:"+ this.name+ " 薪水:"+ this.salary+" 生日:"+ birthday;
}
}
∆ 文件路径
1.1. 绝对路径
以根目录或某盘符开头的路径(或者说完整的路径)
例如:
- c:/a.txt (Windows操作系统中)
- c:/xxx/a.txt (Windows操作系统中)
- /var/xx/aa.txt (Linux操作系统中)
绝对路径的问题: 比如C:\abc\a.properties文件路径,该路径在windows上执行没有 问题,但是如果把该项目移动到linux上面执行 ,该路径就会出现问题了,因为在linux上面没有c盘的,只有根目录\。
1.2. 相对路径
相对于当前路径的一个路径。例如当前文件夹为c:/abc时:相对路径a.txt表示c:/abc/a.txt,相对路径xx/a.txt = c:/abc/xx/a.txt
. 表示当前文件夹
.. 表示上级文件夹
相对路径存在的问题:相对路径是相对于目前执行class文件的时候,控制台所在的路径,这样子也会导致出现问题。
1.3. Java程序中的相对路径
在Java程序中使用File时写相对路径,是指相对于执行java命令时当前所在的文件夹。
测试代码:
在命令行中使用cd命令切换到不同的路径下试试,可以看到以上所说的效果。
在Eclipse中,当前路径是工程的根目录。
1.4. classpath路径
1.4.1. classpath路径说明
在Java程序中,一般情况下使用绝对路径还是相对路径都不太合适,因为Java程序的jar包所放的位置不确定,执行java程序时当前的路径也不确定,所以不合适。一般在Java程序中我们会把资源放到classpath中,然后使用classpath路径查找资源。
Classpath路径:就是使用classpath目前的路径。
1.4.2. 获取classpath中的资源(InputStream)
代码解释
如果经常会发生变化的数据我们可以定义在配置文件上。 比如说:数据库的用户名与密码。
配置文件的路径应该如何写 呢?
绝对路径:一个文件的完整路径信息。一般绝对路径是包含有盘符 的。 绝对路径的缺陷: 因为绝对路径是有盘符开头的,有些系统是没有盘符的。
相对路径: 相对路径是相对于当前程序的路径。当前路径就是执行java命令的时候,控制台所在的路径。
类文件路径 :类文件路径就是使用了classpath的路径找对应的资源文件。
如果需要使用到类文件路径首先先要获取到一个Class对象。
*/
public class DBUtil {
static Properties properties ;
static{
try {
properties = new Properties();
//去加载配置文件 /
Class clazz = DBUtil.class;
InputStream inputStream = clazz.getResourceAsStream("/db.properties"); // "/"代表了Classpath的路径。 getResourceAsStream 该方法里面使用的路径就是使用了类文件路径。
properties.load(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("当前路径:"+ new File(".").getAbsolutePath() );
System.out.println("用户名:"+ properties.getProperty("userName")+" 密码:"+properties.getProperty("password"));
}
}