JUnit4.12 入門實例
作者:
Devin 發(fā)布時間:
[ 2016/10/12 14:37:42 ] 推薦標簽:
單元測試 Junit
JUnit4異常和超時測試
被測類:
public class MyMath {
/**
* 遞歸階乘
* @param n
* @return
* @throws Exception
*/
public int factorial(int n) throws Exception {
if (n < 0) throw new Exception("負數(shù)沒有階乘");
else if (n == 1) return 1;
else return n * factorial(n - 1);
}
/**
* 斐波那契數(shù)列
* @param n
* @return
*/
public int fibonacci(int n){
if (n == 1) return 0;
else if (n == 2) return 1;
else return fibonacci(n-1) + fibonacci(n-2);
}
/**
* 冒泡排序
* @param array
*/
public void bubbleSort(int[] array){
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]){
int temp = array[j];
array[j] = array[j + 1];
array[j+1] = temp;
}
}
}
}
public void quickSort(int[] array){
}
}
測試類:
public class MyMathTest {
@Test
public void factorial() throws Exception {
new MyMath().factorial(1);
}
@Test(expected = Exception.class) //測試異常
public void testFactorial() throws Exception{
new MyMath().factorial(-1);
fail("factorial參數(shù)為負數(shù)沒有拋出異常");
}
@Test
public void fibonacci() throws Exception {
new MyMath().fibonacci(1);
}
@Test(timeout = 10000) //測試超時
public void bubbleSort() throws Exception {
int[] array = new int[10000];
int length = array.length;
Random random = new Random();
for (int i = 0; i < length; i++) {
array[i] = random.nextInt(length);
}
new MyMath().bubbleSort(array);
}
}
JUnit4常用斷言方法
assertNull(java.lang.Object object) 檢查對象是否為空
assertNotNull(java.lang.Object object) 檢查對象是否不為空
assertEquals(long expected, long actual) 檢查long類型的值是否相等
assertEquals(double expected, double actual, double delta) 檢查指定精度的double值是否相等
assertFalse(boolean condition) 檢查條件是否為假
assertTrue(boolean condition) 檢查條件是否為真
assertSame(java.lang.Object expected, java.lang.Object actual) 檢查兩個對象引用是否引用同一對象(即對象是否相等)
assertNotSame(java.lang.Object unexpected, java.lang.Object actual) 檢查兩個對象引用是否不引用統(tǒng)一對象(即對象不等)
fail(String string) 在沒有報告的情況下使測試不通過
public class AssertEqualsTest {
@Test
public void testAssertNull(){
String string = null;
assertNull(string);
}
@Test
public void testAssertNotNull(){
String string = "Junit";
assertNotNull(string);
}
@Test
public void testAssertEqualsLong(){
long long1 = 1;
long long2 = 1;
assertEquals(long1,long2);
}
@Test
public void testAssertEqualsDouble(){
double double1 = 1.234;
double double2 = 1.235;
double delta = 0.002;
assertEquals(double1,double2,delta);
}
@Test
public void testAssertTrue(){
List<String> list = new ArrayList<String>();
assertTrue(list.isEmpty());
}
@Test
public void testAssertFalse(){
List<String> list = new ArrayList<String>();
list.add("junit");
assertFalse(list.isEmpty());
}
@Test
public void testAssertSame(){
String string1 = "HelloWorld";
String string2 = "HelloWorld";
assertSame(string1, string2);
}
@Test
public void testAssertNotSame(){
String string1 = "Hello Junit";
String string2 = "Hello World";
assertNotSame(string1, string2);
}
}
JUnit4參數(shù)化測試
Junit 4 參數(shù)化測試 允許通過變化范圍的參數(shù)值來測試方法。參數(shù)化測試可以通過以下簡單的步驟實現(xiàn):
1.對測試類添加注解 @RunWith(Parameterized.class);
2.將需要使用變化范圍參數(shù)值測試的參數(shù)定義為私有變量;
3.使用上一步驟聲明的私有變量作為入?yún),?chuàng)建構造函數(shù);
4.創(chuàng)建一個使用@Parameters注解的公共靜態(tài)方法,它將需要測試的各種變量值通過集合的形式返回;
5.使用定義的私有變量定義測試方法;
被測類:
public class EvenNumberChecker {
public boolean isEven(int i){
if ((i & 1) == 0){
return true;
}else return false;
}
}
測試類:
//第一步
@RunWith(Parameterized.class)
public class EvenNumberCheckerTest {
//第二步
private int inputNumber;
private boolean isEven;
//第三步
public EvenNumberCheckerTest(int inputNumber, boolean isEven){
this.inputNumber = inputNumber;
this.isEven = isEven;
}
//第四步
@Parameterized.Parameters
public static Collection<Object[]> data(){
Object[][] data = new Object[][]{
{2,true},
{5,false},
{7,false},
{4,true}
};
return Arrays.asList(data);
}
//第五步
@Test
public void testEvenNumberChecker(){
System.out.println("inputNumber:" + inputNumber + " isEven:" + isEven);
EvenNumberChecker evenNumberChecker = new EvenNumberChecker();
boolean result = evenNumberChecker.isEven(inputNumber);
assertEquals(isEven, result);
}
}
JUnit4測試套件
Junit 4允許通過使用測試套件類批量運行測試類 . 為一套測試類創(chuàng)建一個測試套件,要為測試類添加以下注解:
@RunWith(Suite.class)
@SuiteClasses(TestClass1.class, TestClass2.class)
當運行時,所有包含在@SuiteClasses注解內(nèi)的所有測試類都會被執(zhí)行。
@RunWith(Suite.class)
@Suite.SuiteClasses({AnnotationTest.class, EvenNumberCheckerTest.class})
public class SuiteTest {
}
總結
隨著團隊的完善和產(chǎn)品用戶量的增長,對軟件產(chǎn)品質量的要求越來越高,完善和系統(tǒng)的測試是產(chǎn)品質量強大的保障。本文通過為什么要做單元測試、JUnit簡介、單元測試規(guī)范、JUnit4常用注解、JUnit4異常和超時測試、JUnit4常用斷言方法、JUnit4參數(shù)化測試、JUnit4測試套件等八方面的內(nèi)容概要介紹了使用JUnit進行單元測試的相關方法,接下來隨著JUnit5的到來,一個即將重新定義JVM測試方法的版本,我也繼續(xù)完善JUnit進階內(nèi)容!