完整的循環(huán)測試代碼如下:
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
* @創(chuàng)建時間: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);//除數(shù)不能為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
* @創(chuàng)建時間: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);//除數(shù)不能為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不再受命名的限制,所以應(yīng)該劃分更細粒度的測試來完成,一個方法的正確,異常,錯誤及邊界數(shù)據(jù)完全可以分開來寫測試方法。由于大部分情況資源只用加載和釋放一次足夠,大大提高的測試的速度,再也不會有以前那樣點開測試然后去泡咖啡的情況出現(xiàn)了~~呵呵~~