1.Junit介绍
1.概述:Junit是一个单元测试框架,可以代替main方法去执行其他的方法
2.作用:可以单独执行一个方法,测试该方法是否能跑通
3.注意:Junit是第三方工具,所以使用之前需要导入jar包
2.Junit的基本使用
1.导入Junit的jar包
2.定义一个方法,在方法上写注解: @Test
3.执行方法:
a.点击该方法左边的绿色按钮,点击run执行 -> 单独执行一个指定的方法
b.如果想要执行所有带@Test的方法,点击类名左边绿色按钮,点击run执行 -> 执行当前类中所有带@Test的方法
3.Junit的注意事项
1.@Test不能修饰static方法
2.@Test不能修饰带参数的方法
3.@Test不能修饰带返回值的方法
4.Junit相关注解
@Before:在@Test之前执行,有多少个@Test执行,@Before就执行多少次->都是用作初始化一些数据
@After:在@Test之后执行,有多少个@Test执行,@After就执行多少次-> 都是用作释放资源使用
5.@Test怎么使用
public class Demo03Junit {
/* public static void main(String[] args) {
CategoryController cc = new CategoryController();
int result = cc.add("蔬菜");
System.out.println("result = " + result);
List<String> list = cc.findAllCategory();
System.out.println(list);
}*/
/**
* 此方法专门测添加功能
*/
@Test
public void add(){
CategoryController cc = new CategoryController();
int result = cc.add("蔬菜");
System.out.println("result = " + result);
}
/**
* 此方法专门测查询所有功能
*/
@Test
public void findAllCategory(){
CategoryController cc = new CategoryController();
List<String> list = cc.findAllCategory();
System.out.println(list);
}
}
@BeforeClass:在@Test之前执行,只执行一次,可以修饰静态方法
@AfterClass:@Test之后执行,只执行一次,可以修饰静态方法