Junit

JUnit 4 描述

import org.junit.* 用于导入下列注解。
@Test 将方法标记为测试方法。
@Before 在每次测试之前执行,一般用于准备测试环境(初始化类等)。
@After 在每次测试之后执行,用于清理测试环境 (例如删除临时数据,还原默认值等)。 它也可以拥有清理内存( It can also save memory by cleaning up expensive memory structures.)。
@BeforeClass 在所有测试之前,执行一次。它一般用于执行time intensive activities,例如连接数据库等。使用该注解标记的方法需要定义为static void。
@AfterClass 在所有的测试执行完成之后执行一次。 它一般用于清理一些 activities, 例如断开数据连接。使用该注解标记的方法需要定义为static void
@Ignore or @Ignore(“Why disabled”) 标记该注解的测试方法是被禁用的。这对于实际代码做了修改而测试代码没有修改的情况是非常有用的,或者由于这条测试执行时间过长先不将其包含在测试中,最好是提供一下不去测试的原因。
@Test (expected = Exception.class) 如果这个测试方法不抛出赋值的异常(Exception.class)将会失败。
@Test(timeout=100) 如果这个测试方法执行超过100毫秒将会失败。
断言语句(Assert statements)
JUnit提供静态方法,通过Assert类来测试某些条件。这些断言语句通常以assert开头。他们允许你指定错误信息、预期结果和实际结果。断言方法将测试返回的实际值和预期值相比较。如果比较失败就会抛出AssertionException异常。

下表简单的介绍了这些方法。[]中的参数是可选的字符串类型。

语句 描述
fail([message]) 让这个测试方法失败,可能用于检查代码中某个部分是否未执行,或者在执行测试代码之前是否存在失败的测试。这个message参数是可选的。
assertTrue([message,] boolean condition) 验证boolean条件为true。
assertFalse([message,] boolean condition) 验证boolean条件为false。
assertEquals([message,] expected, actual) 验证expected和actual相同。注:对于数组则检查的是引用而不是数组的内容。
assertEquals([message,] expected, actual, tolerance) 验证float或者double匹配。 大家知道计算机表示浮点型数据都有一定的偏差,所以哪怕理论上他们是相等的,但是用计算机表示出来则可能不是,所以这里运行传入一个偏差值。如果两个数的差异在这个偏差值之内,则测试通过,否者测试失败。
assertNull([message,] object) 验证object为null。
assertNotNull([message,] object) 验证object不为null。
assertSame([message,] expected, actual) 验证expected和actual是同一个对象。
assertNotSame([message,] expected, actual) 验证expected和actual不是同一个对象。

参数化测试

@RunWith

当类被@RunWith注解修饰,或者类继承了一个被该注解修饰的类,JUnit将会使用这个注解所指明的运行器(runner)来运行测试,而不使用JUnit默认的运行器。
  要进行参数化测试,需要在类上面指定如下的运行器:
@RunWith (Parameterized.class)
然后,在提供数据的方法上加上一个@Parameters注解,这个方法必须是静态static的,并且返回一个集合Collection。

Junit4 中通过 Parameterized 运行器实现参数化测试。
当执行参数化测试类时,实例的测试方法和测试数据元素将在测试示例创建时交叉连接到一起。
下面是测试菲波那切数的测试方法:

import static org.junit.Assert.assertEquals;
 
import java.util.Arrays;
import java.util.Collection;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
 
@RunWith(Parameterized.class)
public class FibonacciTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {     
                 { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }  
           });
    }
 
    private int fInput;
 
    private int fExpected;
 
    public FibonacciTest(int input, int expected) {
        fInput= input;
        fExpected= expected;
    }
 
    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.compute(fInput));
    }
}
public class Fibonacci {
    public static int compute(int n) {
    	int result = 0;
    	
        if (n <= 1) { 
        	result = n; 
        } else { 
        	result = compute(n - 1) + compute(n - 2); 
        }
        
        return result;
    }
}

每个FibonacciTest类的示例都将通过两个参数的构造器来创建。而这两个参数将通过带有@Parameters注解的方法来获取。
除了构造器注入之外,@Parameters注解支持属性注入

import static org.junit.Assert.assertEquals;
 
import java.util.Arrays;
import java.util.Collection;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
 
@RunWith(Parameterized.class)
public class FibonacciTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
                 { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }  
           });
    }
 
    @Parameter // first data value (0) is default
    public /* 不能为 private */ int fInput;
 
    @Parameter(1)
    public /* 不能为 private */ int fExpected;
 
    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.compute(fInput));
    }
}
 
public class Fibonacci {
    ...
}

注意,目前只支持公共属性的注入

单个参数的测试
如果你的测试只需要单个参数,则不需要将其包装成数组。这种情况下可以提供一个迭代器或对象数组。

@Parameters
public static Iterable<? extends Object> data() {
    return Arrays.asList("first test", "second test");
}@Parameters
public static Object[] data() {
    return new Object[] { "first test", "second test" };
}

识别每个测试用例
为了更容易地区分参数化测试中每个测试用例,你可以在@Parameters 注解上提供一个名称。
此名称可以包含占位符,该占位符在运行时将会被替换。
{index}: 当前参数的索引
{0}, {1}, …: 第一个参数,第二个参数等,参数值. 注意:单引号 ’ 应该被转义成两个单引号 ‘’.

例子

import static org.junit.Assert.assertEquals;
 
import java.util.Arrays;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
 
@RunWith(Parameterized.class)
public class FibonacciTest {
 
    @Parameters(name = "{index}: fib({0})={1}")
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] { 
                 { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
           });
    }
 
    private int input;
    private int expected;
 
    public FibonacciTest(int input, int expected) {
        this.input = input;
        this.expected = expected;
    }
 
    @Test
    public void test() {
        assertEquals(expected, Fibonacci.compute(input));
    }
}
 
public class Fibonacci {
    ...
}

上面这个例子,参数化 运行器 创建如 [3: fib(3)=2] 这种名称。如果你没有指定名称,默认使用当前参数的索引。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值