一、junit使用
1.测试和业务分离放在俩个包中
起名:公司域名倒置+Test
测试都放在这个包下
测试类的名字:****Test
测试类的定义:方法独立运行,不依托与main方法
建议:
名字:testAdd() testSub()
参数:无参
返回值:void
2.测试方法定义以后,不能直接独立运行,必须要在方法前加入一个注解:@Test
3.导入Junite依赖的环境
public class calculator {
//加法
public int add(int a,int b){
return a+b;
}
//减法
public int sub(int a,int b){
return a-b;
}
}
public class CalculatorTest {
//测试Add
@Test
public void testAdd() {
calculator cal =new calculator();
int add = cal.add(10, 20);
System.out.println(add);
}
//测试Sub
@Test
public void testSub(){
calculator cal=new calculator();
int sub = cal.sub(20, 10);
System.out.println(sub);
}
}
4.判断程序是否逻辑正确,引入断言
//加入断言,预测结果,判断预测结果和实际结果是否一致
Assert.assertEquals(40,add);
//第一个参数预测结果,第二个参数实际结果
二、before、after注解
@before
某个方法中,加入了@before注解后,那么这个方法中的功能会在测试方法前先执行,一般会加入一些申请资源的代码:申请数据库资源,申请IO资源,申请网络资源
@after
某个方法中,加入了@after注解后,那么这个方法的功能会在测试方法在执行后先执行,一般会释放一些资源的代码:释放数据库资源,释放IO流资源,释放网络资源
@Before
public void init(){
System.out.println("方法执行开始了");
}
@After
public void close(){
System.out.println("方法执行结束了");
}
//测试Add
@Test
public void testAdd() {
System.out.println("加法测试");
calculator cal =new calculator();
int add = cal.add(20, 20);
System.out.println(add);//程序运行结可以不关注果
//加入断言,预测结果,判断预测结果和实际结果是否一致
Assert.assertEquals(40,add);
//第一个参数预测结果,第二个参数实际结果
}
//测试Sub
@Test
public void testSub(){
calculator cal=new calculator();
int sub = cal.sub(20, 10);
System.out.println(sub);
}
三、注解
1.定义
注解是代码里的特殊标记,这些标记可以在编译类加载运行时被读取,并执行相应的处理,通过使用注解,可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充信息,代码分析工具,开发工具和部署工具可以哦那个过这些补充信息惊醒验证或者进行部署。
案例
import com.twx.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("方法执行开始了");
}
@After
public void close(){
System.out.println("方法执行结束了");
}
//测试Add
@Test
public void testAdd() {
System.out.println("加法测试");
calculator cal =new calculator();
int add = cal.add(20, 20);
System.out.println(add);//程序运行结可以不关注果
//加入断言,预测结果,判断预测结果和实际结果是否一致
Assert.assertEquals(40,add);
//第一个参数预测结果,第二个参数实际结果
}
2.文档注解
public class Person {
/**
*
* @param num1 就餐人数
* @param num2 及菜一汤
*/
public void eat(int num1,int num2){
}
/**
*
* @param age 年龄
* @return int 返回值
* @exception RuntimeException 当年龄过大
* @exception IndexOutOfBoundsException 当年龄过小
* @see Student 参照学生类
*/
public int sleep(int age){
new Student();
if(age>100){
throw new RuntimeException();
}
if(age<0){
throw new IndexOutOfBoundsException();
}
return 10;
}
四、JDK内置的三个注解
1.@Overrride:限定重写父类方法,该注解只能用于方法
public class Student extends Person {
@Override //限定重写的方法,若重写有问题,则显示错误
public void eat(){
System.out.println("吃饭");
}
}
2.@Deprecated:用于表示所修饰的元素(类,方法,构造器)过时,通常因为所修饰的结构危险或存在更好的选择
@Deprecated //加入此注解,该方法就会变成一个废弃、过期、过时方法
public void study(){
System.out.println("学习");
}
3.@SuppressWarnings:抑制编译器警告
public static void main(String[] args) {
@SuppressWarnings("unused")
int age=10;
}
五、自定义注解
内部没有定义配置参数的注解叫做标记
内部定义配置参数的注解叫做元数据
六、元注解
定义:用于修饰其他注解的注解
1.retention元注解
RetentionPolicy.SOURCE:只在源文件中有效(源文件保留),编译器直接丢弃这种策略的注释,在class文件中不会保留注解信息
RetentionPolicy.CLASS:在class文件中有效,保留在class文件中,但当运行时,不会继续加载,不会保留在内存中,jvm不会保留注释。如果注解没有加Retention元注解,那么相当于默认的注解就是这种状态
RetentionPolicy.RUNTIME:在运行时有用,运行时会保留注释,记载在内存中了,程序可以通过反射获取该注解
2.Target元注解
@MyAnnotation
public class Student {
@MyAnnotation
private int age;
@MyAnnotation
public Student() {
}
@MyAnnotation
public void eat(){
}
}
3.Documented元注解
Deprecated会在javac.doc
4.元注解inherited
修饰的注解类将具有继承性,如果这个类使用了被@inherite修饰的注释,则他的子类将自动具有该注解
@Inherited
public @interface MtAnnotation {
}
父类
@MyAnnotation("a")
public class Person {
}
子类
public class Student extends Person{
}
父类自动使用了注解