1.使用Junit的步骤
(1)导入jar包
①Junit.jar
②hamcrest-core.jar(依赖包)
(2)新建一个测试类,编写测试方法,在方法前加上@Test注解,右键Run as -> Junit Test执行,即可得到测试结果
快捷键:ctrl + F11
2.其他注解
(1)@Before
放在任一方法之前,则该方法将在所有测试方法执行之前执行,一般该注解修饰初始化方法(用户初始化将要测试的代码)
(2)@After
放在任一方法之前,则该方法将在所有测试方法执行之后执行,一般该注解修饰释放资源的方法(用户释放 测试方法中申请的资源)
3.代码
package com.spring.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class JUnitTest {
private StringBuilder sb;
@Test
public void test01() {
System.out.println("------------------------");
System.out.println("sb == null:" + (sb==null));
sb.append("test01");
System.out.println(sb.toString());
}
@Before
public void doBefore() {
System.out.println("------------------------");
System.out.println("sb == null:" + (sb==null));
sb = new StringBuilder();
}
@Test
public void test02() {
System.out.println("------------------------");
System.out.println("sb == null:" + (sb==null));
sb.append("test02");
System.out.println(sb.toString());
}
@Test
public void test() {
test01();
test02();
}
@After
public void doAfter() {
System.out.println("------------------------");
sb = null;
System.out.println("sb==null:"+(sb==null));
}
}