一、Junit单元测试
1.Junit单元测试概述
2.Junit单元测试使用步骤
传统测试功能
package cn.itcast.junit;
public class Calculator {
public int add (int a , int b){
return a - b;
}
public int sub (int a , int b){
return a - b;
}
}
package cn.itcast.junit;
public class CalculatorTest {
public static void main(String[] args) {
Calculator c = new Calculator();
int result = c.sub(1, 1);
System.out.println(result);
}
}
Junit单元测试功能
package cn.itcast.junit;
public class Calculator {
public int add (int a , int b){
return a - b;
}
public int sub (int a , int b){
return a - b;
}
}
package cn.itcast.test;
import cn.itcast.junit.Calculator;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class CalculatorTest {
@Test
public void testAdd(){
Calculator c = new Calculator();
int result = c.add(1, 2);
Assert.assertEquals(3,result);
}
@Test
public void testSub(){
Calculator c = new Calculator();
int result = c.sub(1, 2);
Assert.assertEquals(-1,result);
}
}
3.Junit@Before&@After
package cn.itcast.junit;
public class Calculator {
public int add (int a , int b){
return a - b;
}
public int sub (int a , int b){
return a - b;
}
}
package cn.itcast.test;
import cn.itcast.junit.Calculator;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class CalculatorTest {
@Before
public void init(){
System.out.println("init...");
}
@After
public void close(){
System.out.println("close...");
}
@Test
public void testAdd(){
System.out.println("testAdd...");
Calculator c = new Calculator();
int result = c.add(1, 2);
Assert.assertEquals(3,result);
}
@Test
public void testSub(){
Calculator c = new Calculator();
int result = c.sub(1, 2);
System.out.println("testSub....");
Assert.assertEquals(-1,result);
}
}
二、反射
1.反射概述
2.反射获取字节码Class对象的三种方式
package cn.itcast.domain;
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = 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
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package cn.itcast.domain;
public class Student {
}
package cn.itcast.reflect;
import cn.itcast.domain.Person;
import cn.itcast.domain.Student;
public class ReflectDemo1 {
public static void main(String[] args) throws Exception {
Class cls1 = Class.forName("cn.itcast.domain.Person");
System.out.println(cls1);
Class cls2 = Person.class;
System.out.println(cls2);
Person p = new Person();
Class cls3 = p.getClass();
System.out.println(cls3);
System.out.println(cls1 == cls2);
System.out.println(cls1 == cls3);
Class c = Student.class;
System.out.println(c == cls1);
}
}
3.反射Class对象功能概述
4.反射Class对象功能获取Field
package cn.itcast.domain;
public class Person {
private String name;
private int age;
public String a;
protected String b;
String c;
private String d;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = 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
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", a='" + a + '\'' +
", b='" + b + '\'' +
", c='" + c + '\'' +
", d='" + d + '\'' +
'}';
}
}
package cn.itcast.reflect;
import cn.itcast.domain.Person;
import java.lang.reflect.Field;
public class ReflectDemo2 {
public static void main(String[] args) throws Exception {
Class personClass = Person.class;
Field[] fields = personClass.getFields();
for (Field field : fields) {
System.out.println(field);
}
System.out.println("------------");
Field a = personClass.getField("a");
Person p = new Person();
Object value = a.get(p);
System.out.println(value);
a.set(p,"张三");
System.out.println(p);
System.out.println("===================");
Field[] declaredFields = personClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
Field d = personClass.getDeclaredField("d");
d.setAccessible(true);
Object value2 = d.get(p);
System.out.println(value2);
}
}
5.反射Class对象功能获取Constructor
package cn.itcast.domain;
public class Person {
private String name;
private int age;
public String a;
protected String b;
String c;
private String d;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = 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
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", a='" + a + '\'' +
", b='" + b + '\'' +
", c='" + c + '\'' +
", d='" + d + '\'' +
'}';
}
}
package cn.itcast.reflect;
import cn.itcast.domain.Person;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class ReflectDemo3 {
public static void main(String[] args) throws Exception {
Class personClass = Person.class;
Constructor constructor = personClass.getConstructor(String.class, int.class);
System.out.println(constructor);
Object person = constructor.newInstance("张三", 23);
System.out.println(person);
System.out.println("----------");
Constructor constructor1 = personClass.getConstructor();
System.out.println(constructor1);
Object person1 = constructor1.newInstance();
System.out.println(person1);
Object o = personClass.newInstance();
System.out.println(o);
}
}
6.反射Class对象功能获取Method
package cn.itcast.domain;
public class Person {
private String name;
private int age;
public String a;
protected String b;
String c;
private String d;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = 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
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", a='" + a + '\'' +
", b='" + b + '\'' +
", c='" + c + '\'' +
", d='" + d + '\'' +
'}';
}
public void eat(){
System.out.println("eat...");
}
public void eat(String food){
System.out.println("eat..."+food);
}
}
package cn.itcast.reflect;
import cn.itcast.domain.Person;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class ReflectDemo4 {
public static void main(String[] args) throws Exception {
Class personClass = Person.class;
Method eat_method = personClass.getMethod("eat");
Person p = new Person();
eat_method.invoke(p);
Method eat_method2 = personClass.getMethod("eat", String.class);
eat_method2.invoke(p,"饭");
System.out.println("-----------------");
Method[] methods = personClass.getMethods();
for (Method method : methods) {
System.out.println(method);
String name = method.getName();
System.out.println(name);
}
String className = personClass.getName();
System.out.println(className);
}
}
7.反射案例
**pro.properties:**
className=cn.itcast.domain.Student
methodName=sleep
package cn.itcast.domain;
public class Person {
private String name;
private int age;
public String a;
protected String b;
String c;
private String d;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = 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
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", a='" + a + '\'' +
", b='" + b + '\'' +
", c='" + c + '\'' +
", d='" + d + '\'' +
'}';
}
public void eat(){
System.out.println("eat...");
}
public void eat(String food){
System.out.println("eat..."+food);
}
}
package cn.itcast.domain;
public class Student {
public void sleep(){
System.out.println("sleep...");
}
}
package cn.itcast.reflect;
import cn.itcast.domain.Person;
import cn.itcast.domain.Student;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;
public class ReflectTest {
public static void main(String[] args) throws Exception {
Properties pro = new Properties();
ClassLoader classLoader = ReflectTest.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("pro.properties");
pro.load(is);
String className = pro.getProperty("className");
String methodName = pro.getProperty("methodName");
Class cls = Class.forName(className);
Object obj = cls.newInstance();
Method method = cls.getMethod(methodName);
method.invoke(obj);
}
}
三、注解
1.注解概念
package cn.itcast.annotation;
public class AnnoDemo1 {
public int add(int a, int b ){
return a + b;
}
}
注意:
- 生成doc文档后,点击
index.html 文件可以查看 - 注意文件格式是否是ANSI格式文件(中文操作系统GBK)。
2.注解JDK内置注解
package cn.itcast.annotation;
import java.util.Date;
@SuppressWarnings("all")
public class AnnoDemo2 {
@Override
public String toString() {
return super.toString();
}
@Deprecated
public void show1(){
}
public void show2(){
}
public void demo(){
show1();
Date date = new Date();
}
}
3.注解自定义注解格式&本质
package cn.itcast.annotation;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
public @interface MyAnno {
public abstract String show();
}
4.注解自定义注解属性定义
属性的返回值类型有下列取值:
package cn.itcast.annotation;
public enum Person {
P1,P2;
}
package cn.itcast.annotation;
public @interface MyAnno2 {
}
package cn.itcast.annotation;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
public @interface MyAnno {
int show();
String show2();
Person per();
MyAnno2 anno2();
String[] strs();
}
定义了属性,在使用时需要给属性赋值:
定义了属性,在使用时需要给属性赋值(1):default
package cn.itcast.annotation;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
public @interface MyAnno {
int age();
String name() default "张三";
}
package cn.itcast.annotation;
@MyAnno(age = 12)
public class Worker {
}
定义了属性,在使用时需要给属性赋值(2):value
package cn.itcast.annotation;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
public @interface MyAnno {
int value();
}
package cn.itcast.annotation;
@MyAnno(12)
public class Worker {
}
定义了属性,在使用时需要给属性赋值(3):多个属性赋值
package cn.itcast.annotation;
public enum Person {
P1,P2;
}
package cn.itcast.annotation;
public @interface MyAnno2 {
}
package cn.itcast.annotation;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
public @interface MyAnno {
int value();
Person per();
MyAnno2 anno2();
String[] strs();
}
package cn.itcast.annotation;
@MyAnno(value=12,per = Person.P1,anno2 = @MyAnno2,strs="bbb")
public class Worker {
}
5.注解自定义注解元注解
@Target:
package cn.itcast.annotation;
import java.lang.annotation.*;
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
public @interface MyAnno3 {
}
package cn.itcast.annotation;
@MyAnno(value=12,per = Person.P1,anno2 = @MyAnno2,strs="bbb")
@MyAnno3
public class Worker {
@MyAnno3
public String name = "aaa";
@MyAnno3
public void show(){
}
}
@Retention:
@Retention(RetentionPolicy.SOURCE):当前被描述的注解,不会保留到class字节码文件中 @Retention(RetentionPolicy.CLASS):当前被描述的注解,会保留到class字节码文件中 @Retention(RetentionPolicy.RUNTIME):当前被描述的注解,会保留到class字节码文件中,并被JVM读取到
package cn.itcast.annotation;
import java.lang.annotation.*;
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno3 {
}
@Documented:
package cn.itcast.annotation;
import java.lang.annotation.*;
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnno3 {
}
@Inherited:
package cn.itcast.annotation;
import java.lang.annotation.*;
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyAnno3 {
}
package cn.itcast.annotation;
@MyAnno(value=12,per = Person.P1,anno2 = @MyAnno2,strs="bbb")
@MyAnno3
public class Worker {
@MyAnno3
public String name = "aaa";
@MyAnno3
public void show(){
}
}
package cn.itcast.annotation;
public class Teacher extends Worker {
}
6.注解解析注解
package cn.itcast.annotation;
public class Demo1 {
public void show(){
System.out.println("demo1...show...");
}
}
package cn.itcast.annotation;
public class Demo2 {
public void show(){
System.out.println("demo2...show...");
}
}
package cn.itcast.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Pro {
String className();
String methodName();
}
package cn.itcast.annotation;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;
@Pro(className = "cn.itcast.annotation.Demo1",methodName = "show")
public class ReflectTest {
public static void main(String[] args) throws Exception {
Class<ReflectTest> reflectTestClass = ReflectTest.class;
Pro an = reflectTestClass.getAnnotation(Pro.class);
String className = an.className();
String methodName = an.methodName();
System.out.println(className);
System.out.println(methodName);
Class cls = Class.forName(className);
Object obj = cls.newInstance();
Method method = cls.getMethod(methodName);
method.invoke(obj);
}
}
7.注解案例简单的测试框架
package cn.itcast.annotation.demo;
public class Calculator {
@Check
public void add(){
String str = null;
str.toString();
System.out.println("1 + 0 =" + (1 + 0));
}
@Check
public void sub(){
System.out.println("1 - 0 =" + (1 - 0));
}
@Check
public void mul(){
System.out.println("1 * 0 =" + (1 * 0));
}
@Check
public void div(){
System.out.println("1 / 0 =" + (1 / 0));
}
public void show(){
System.out.println("永无bug...");
}
}
package cn.itcast.annotation.demo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Check {
}
package cn.itcast.annotation.demo;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestCheck {
public static void main(String[] args) throws IOException {
Calculator c = new Calculator();
Class cls = c.getClass();
Method[] methods = cls.getMethods();
int number = 0;
BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));
for (Method method : methods) {
if(method.isAnnotationPresent(Check.class)){
try {
method.invoke(c);
} catch (Exception e) {
number ++;
bw.write(method.getName()+ " 方法出异常了");
bw.newLine();
bw.write("异常的名称:" + e.getCause().getClass().getSimpleName());
bw.newLine();
bw.write("异常的原因:"+e.getCause().getMessage());
bw.newLine();
bw.write("--------------------------");
bw.newLine();
}
}
}
bw.write("本次测试一共出现 "+number+" 次异常");
bw.flush();
bw.close();
}
}
|