5) 参数测试
修改 testMultiple
Java代码
//@Ignore("忽略乘法测试")
@Test
public void testMultiple() {
assertEquals(result,Math.multiple(faciend,multiplicator));
}
//@Ignore("忽略乘法测试")
@Test
public void testMultiple() {
assertEquals(result,Math.multiple(faciend,multiplicator));
}
编写参数方法:
Java代码
@Parameters
public static Collection multipleValues() {
return Arrays.asList(new Object[][] {
{3, 2, 6 },
{4, 3, 12 },
{21, 5, 105 },
{11, 22, 242 },
{8, 9, 72 }});
}
@Parameters
public static Collection multipleValues() {
return Arrays.asList(new Object[][] {
{3, 2, 6 },
{4, 3, 12 },
{21, 5, 105 },
{11, 22, 242 },
{8, 9, 72 }});
}
说明:
需要使用@Parameters标签注解一个静态的返回集合对象的方法
增加成员变量和构造函数:
Java代码
int faciend;
int multiplicator;
int result;
public MathTest(int faciend, int multiplicator, int result) {
this.faciend = faciend;
this.multiplicator = multiplicator;
this.result = result;
}
int faciend;
int multiplicator;
int result;
public MathTest(int faciend, int multiplicator, int result) {
this.faciend = faciend;
this.multiplicator = multiplicator;
this.result = result;
}
最后在给测试类增加如下注释:
Java代码
@RunWith(Parameterized.class)
完整的循环测试代码如下:
Java代码
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
* @author bulargy.j.bai
* @创建时间:Mar 11, 2008
* @描述:
*/
@RunWith(Parameterized.class)
public class MathTest {
int faciend;
int multiplicator;
int result;
public MathTest(int faciend, int multiplicator, int result) {
this.faciend = faciend;
this.multiplicator = multiplicator;
this.result = result;
}
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
/**
* Test method for {@link org.bj.util.Math#divide(int, int)}.
*/
@Test(expected=ArithmeticException.class)
public void testDivide() {
assertEquals(3,Math.divide(9,3));
assertEquals(3,Math.divide(10,3));
Math.divide(10,0);//除数不能为0,会抛出异常
}
/**
* Test method for {@link org.bj.util.Math#multiple(int, int)}.
*/
//@Ignore("忽略乘法测试")
@Test
public void testMultiple() {
assertEquals(result,Math.multiple(faciend,multiplicator));
}
@Parameters
public static Collection multipleValues() {
return Arrays.asList(new Object[][] {
{3, 2, 6 },
{4, 3, 12 },
{21, 5, 105 },
{11, 22, 242 },
{8, 9, 72 }});
}
}
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
* @author bulargy.j.bai
* @创建时间:Mar 11, 2008
* @描述:
*/
@RunWith(Parameterized.class)
public class MathTest {
int faciend;
int multiplicator;
int result;
public MathTest(int faciend, int multiplicator, int result) {
this.faciend = faciend;
this.multiplicator = multiplicator;
this.result = result;
}
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
/**
* Test method for {@link org.bj.util.Math#divide(int, int)}.
*/
@Test(expected=ArithmeticException.class)
public void testDivide() {
assertEquals(3,Math.divide(9,3));
assertEquals(3,Math.divide(10,3));
Math.divide(10,0);//除数不能为0,会抛出异常
}
/**
* Test method for {@link org.bj.util.Math#multiple(int, int)}.
*/
//@Ignore("忽略乘法测试")
@Test
public void testMultiple() {
assertEquals(result,Math.multiple(faciend,multiplicator));
}
@Parameters
public static Collection multipleValues() {
return Arrays.asList(new Object[][] {
{3, 2, 6 },
{4, 3, 12 },
{21, 5, 105 },
{11, 22, 242 },
{8, 9, 72 }});
}
}
OK,大功告成。测试看看吧,测试类跑了5次~~。
大概就这么多体会了,总得来说JUnit4以后测试还是很方便的,顺便这个是仅仅是为了做例子,实际使用中由于JUnit4不再受命名的限制,所以应该划分更细粒度的测试来完成,一个方法的正确,异常,错误及边界数据完全可以分开来写测试方法。由于大部分情况资源只用加载和释放一次就足够,大大提高的测试的速度,再也不会有以前那样点开测试然后去泡咖啡的情况出现了~~呵呵