java--单元测试、内省

junit(单元测试框架)

junit要注意的细节:

1. 如果使用junit测试一个方法的时候,在junit窗口上显示绿条那么代表测试正确,
如果是出现了红条,则代表该方法测试出现了异常不通过。
2. 如果点击方法名、 类名、包名、 工程名运行junit分别测试的是对应的方法,类、 包中 的所有类的test方法,工程中的所有test方法。
3.  @Test测试的方法不能是static修饰与不能带有形参(可以写一个测试方法调用想测试的那个方法)。
4. 如果测试一个方法的时候需要准备测试的环境或者是清理测试的环境,那么可以@Before、 @After 、@BeforeClass、 @AfterClass这四个注解。
@Before、 @After 是在每个测试方法测试的时候都会调用一次, @BeforeClass、 @AfterClass是在所有的测试方法测试之前与测试之后调用一次而已。

@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));

}   

Tool类

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;
}

}

Tool测试类

//测试类
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);
    }
}
}

//准备测试的环境
//@Before  因为有2个测试方法,此种注解会准备2此测试环境,这个方法不是静态方法
@BeforeClass  此种注解即使有多个测试方法,只会准备1次测试环境,但这个方法必须是静态方法
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("清理测试环境..");
}

内省

内省主要解决 的问题: 把对象的属性数据封装 到对象中。

//实体类---javaBean
public class Person {

private int id;

private String name;

public Person(int id, String name) {
    super();
    this.id = id;
    this.name = name;
}


public Person(){}



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;
}


@Override
public String toString() {

    return "编号:"+ this.id+" 姓名:"+ this.name;
}

}

@Test
public void getAllProperty() throws IntrospectionException{
    //Introspector 内省类
    BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
    //通过BeanInfo获取所有的属性描述其
    PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); //获取一个类中的所有属性描述器
    for(PropertyDescriptor p : descriptors){
        System.out.println(p.getReadMethod()); //get方法
    }

}


@Test
public  void testProperty() throws Exception{
    Person p = new Person();
    //属性描述器
    PropertyDescriptor descriptor = new PropertyDescriptor("id", Person.class);
    //获取属性对应的get或者是set方法设置或者获取属性了。
    Method  m = descriptor.getWriteMethod();  //获取属性的set方法。
    //执行该方法设置属性值
    m.invoke(p,110);

    Method readMethod = descriptor.getReadMethod(); //是获取属性的get方法

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

BeanUtils

BeanUtils主要解决 的问题: 把对象的属性数据封装 到对象中。

BeanUtils的好处:

1. BeanUtils设置属性值的时候,如果属性是基本数据 类型,BeanUtils会自动帮我转换数据类型。
2. BeanUtils设置属性值的时候底层也是依赖于get或者Set方法设置以及获取属性值的。
3. BeanUtils设置属性值,如果设置的属性是其他的引用 类型数据,那么这时候必须要注册一个类型转换器。

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;
}

}

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);
}

路径

绝对路径

一个文件的完整路径信息。一般绝对路径是包含有盘符 的。 绝对路径的缺陷: 因为绝对路径是有盘符开头的,有些系统是没有盘符的。

相对路径

相对路径是相对于当前程序的路径。当前路径就是执行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"));

}

}

猜你喜欢

转载自blog.csdn.net/qq_17503037/article/details/80022046