额,久了不碰,又忘。。。还是记录下
1.
@BeforeClass修饰的方法会在所有方法被调用前被执行,
而且该方法是静态的,所以当测试类被加载后接着就会运行它,
而且在内存中它只会存在一份实例,它比较适合加载配置文件。
@BeforeClass修饰的方法运行结束之后,测试器会为每个测试执行以下步骤:
- 创建一个新的测试集实例,也就是说,和所有的Java类一样,构造函数中所有的代码都会执行。这也是为什么每个@Test标注的测试方法,都可以单独的运行的原因见代码测试1
- @Before标注的方法在对象构造之后执行(因为每一个测试方法之前都会执行该方法,故可以使用该方法进行一些设置)
- 运行测试方法
- @After会在每个测试方法运行结束后(无论测试是否成功)执行一次(故可以使用该方法清理测试中产生的一些垃圾,比如一些数据库或者文件操作留下的痕迹)。
测试执行顺序以及验证每个测试方法都构造一个实例见代码测试1
代码测试1
import org.junit.*;
import static org.junit.Assert.assertEquals;
/**
* <p>pakage: PACKAGE_NAME,descirption:</p>
*
* @author wanghai
* @version V1.0
* @since <pre>2018/6/17 下午9:32</pre>
*/
public class JUnitDemo2 {
private static int counter = 0;
@BeforeClass
public static void myBeforeClassMethod() {
assertEquals(0,counter);
counter++;
}
@Before
public void myBefore(){
assertEquals(1,counter++);
}
@Test
public void myTest1(){
assertEquals(2,counter++);
}
@Test
public void myTest2(){
assertEquals(2,counter++);
}
@After
public void myAfter(){
assertEquals(3,counter++);
}
@AfterClass
public static void myAfterClass(){
assertEquals(4,counter++);
}
}
2.当一个方法的源码已经修改过,但是测试方法还没有来得及修改时,我们可以使用@Ignore方法(被标记的方法在测试时会跳过);也可以对整个类标记,告诉JUnit运行器跳过这一个测试集
3.AfterClass 和 BeforeClass 是public static void修饰,其他是public void
4.assertEquals等assert系列方法都有两个重载实现,推荐使用带“message“的,会在测试fail时打印消息
测试2
/**
* <p>pakage: PACKAGE_NAME,descirption:</p>
*
* @author wanghai
* @version V1.0
* @since <pre>2018/6/17 下午8:47</pre>
*/
public class JUnitDemo {
public static int add1(){
return 1;
}
public static int add2(){
return 2;
}
}
import org.junit.*;
import static org.junit.Assert.assertEquals;
/**
* Demo Tester.
*
* @author <wanghai>
* @version 1.0
* @since <pre>05/18/2018</pre>
*/
public class JUnitDemoTest {
/* *
*1. @BeforeClass修饰的方法会在所有方法被调用前被执行,
* 而且该方法是静态的,所以当测试类被加载后接着就会运行它,
* 而且在内存中它只会存在一份实例,它比较适合加载配置文件。
* 2. @AfterClass所修饰的方法通常用来对资源的清理,如关闭数据库的连接
* 3. @Before和@After会在每个测试方法的前后各执行一次。
* */
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("this is beforeClass...");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("this is afterClass...");
}
/**
* 即使在@Before注解方法、@Test注解方法中抛出了异常,
* 所有的@After注解方法依然会被执行
*/
@Before
public void beforeEveryMethod() throws Exception {
System.out.println("this is before...every method");
}
/**
* 即使在@Before注解方法、@Test注解方法中抛出了异常,
* 所有的@After注解方法依然会被执行
*/
@After
public void afterEveryMethod() throws Exception {
System.out.println("this is after...every method");
}
/**
* Method: add1()
*/
@Test
public void testAdd1() throws Exception {
/**
*
*
* 1. Failure一般由单元测试使用的断言方法判断失败所引起的,
* 这经表示测试点发现了问题 ,就是说程序输出的结果和我们预期的不一样。
* 2. error是由代码异常引起的,它可以产生于测试代码本身的错误,
* 也可以是被测试代码中的 一个隐藏的bug
* 3.测试用例不是用来证明你是对的,而是用来证明你没有错。
*/
// java.lang.AssertionError: 不一致,我们希望得到的是1
assertEquals("不一致,我们希望得到的是1",0, JUnitDemo.add1());
}
/**
* Method: add2()
*/
@Test
public void testAdd2() throws Exception {
assertEquals(2, JUnitDemo.add2());
}
}
5.@assertArrayEquals:
如果是object数组,则调用对象的equals方法
6.如果源代码中是抛出异常(或者不予处理),测试实例不catch的话,则如果测试失败,会展示异常栈
public static int test3() throws IndexOutOfBoundsException{
List<Integer> numbers = new ArrayList<>();
// numbers.add(30);
int i = 0;
i = numbers.get(0);
return i;
}
此时会抛出
java.lang.IndexOutOfBoundsException: Index 0 out-of-bounds for length 0
@Test
public void test3(){
assertEquals("不一致,期望得到50", 50, JUnitDemo.test3());
}
但是如果测试中已经catch,如果抛出相应的错,也就是catch成功,则测试是通过的
如果没有catch到,且测试失败,则失败
代码修改如下:
public static int test3(){
List<Integer> numbers = new ArrayList<>();
// numbers.add(30);
int i = 0;
i = numbers.get(0);
return i;
测试:
@Test
public void test3(){
try {
assertEquals("不一致,期望得到50", 50, JUnitDemo.test3());
}catch (IndexOutOfBoundsException e){
System.out.println("==============");
e.printStackTrace();
}
}
但是如果源码就已经catch了错误,则测试是无法catch的
public static int test3(){
List<Integer> numbers = new ArrayList<>();
// numbers.add(30);
int i = 50;
// i = numbers.get(0);
try {
i = numbers.get(0);
}catch (IndexOutOfBoundsException e){
System.out.println("==============");
}
return i;
}
5.上面的都是我“臆想“的。。。预期出现的异常可以这样做
@Test(expected = NoSuchFileFoundException)
但是当测试中可能出现多次抛异常的话,可能会使人有些迷惑
更好的是显式的检查每一行代码
6.对于时间太长的测试,想要跳过时,可以设置超时参数
@Test(timeout = 10000L)
如果该测试不能在10秒内结束,终止该测试
当然,也可以显式的new一个线程来实现超时终止测试