您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > junit
JUnit4.12 入門實(shí)例
作者:Devin 發(fā)布時(shí)間:[ 2016/10/12 14:37:42 ] 推薦標(biāo)簽:單元測(cè)試 Junit

  JUnit4異常和超時(shí)測(cè)試
  被測(cè)類:
public class MyMath {
/**
* 遞歸階乘
* @param n
* @return
* @throws Exception
*/
public int factorial(int n) throws Exception {
if (n < 0) throw new Exception("負(fù)數(shù)沒(méi)有階乘");
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){
}
}
  測(cè)試類:
public class MyMathTest {
@Test
public void factorial() throws Exception {
new MyMath().factorial(1);
}
@Test(expected = Exception.class) //測(cè)試異常
public void testFactorial() throws Exception{
new MyMath().factorial(-1);
fail("factorial參數(shù)為負(fù)數(shù)沒(méi)有拋出異常");
}
@Test
public void fibonacci() throws Exception {
new MyMath().fibonacci(1);
}
@Test(timeout = 10000) //測(cè)試超時(shí)
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)  檢查對(duì)象是否為空
assertNotNull(java.lang.Object object)  檢查對(duì)象是否不為空
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)  檢查兩個(gè)對(duì)象引用是否引用同一對(duì)象(即對(duì)象是否相等)
assertNotSame(java.lang.Object unexpected, java.lang.Object actual)    檢查兩個(gè)對(duì)象引用是否不引用統(tǒng)一對(duì)象(即對(duì)象不等)
fail(String string)  在沒(méi)有報(bào)告的情況下使測(cè)試不通過(guò)
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ù)化測(cè)試
  Junit 4 參數(shù)化測(cè)試 允許通過(guò)變化范圍的參數(shù)值來(lái)測(cè)試方法。參數(shù)化測(cè)試可以通過(guò)以下簡(jiǎn)單的步驟實(shí)現(xiàn):
  1.對(duì)測(cè)試類添加注解 @RunWith(Parameterized.class);
  2.將需要使用變化范圍參數(shù)值測(cè)試的參數(shù)定義為私有變量;
  3.使用上一步驟聲明的私有變量作為入?yún),?chuàng)建構(gòu)造函數(shù);
  4.創(chuàng)建一個(gè)使用@Parameters注解的公共靜態(tài)方法,它將需要測(cè)試的各種變量值通過(guò)集合的形式返回;
  5.使用定義的私有變量定義測(cè)試方法;
  被測(cè)類:
  public class EvenNumberChecker {
  public boolean isEven(int i){
  if ((i & 1) == 0){
  return true;
  }else return false;
  }
  }
  測(cè)試類:
//第一步
@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測(cè)試套件
  Junit 4允許通過(guò)使用測(cè)試套件類批量運(yùn)行測(cè)試類 . 為一套測(cè)試類創(chuàng)建一個(gè)測(cè)試套件,要為測(cè)試類添加以下注解:
@RunWith(Suite.class)
@SuiteClasses(TestClass1.class, TestClass2.class)
當(dāng)運(yùn)行時(shí),所有包含在@SuiteClasses注解內(nèi)的所有測(cè)試類都會(huì)被執(zhí)行。
@RunWith(Suite.class)
@Suite.SuiteClasses({AnnotationTest.class, EvenNumberCheckerTest.class})
public class SuiteTest {
}
  總結(jié)
  隨著團(tuán)隊(duì)的完善和產(chǎn)品用戶量的增長(zhǎng),對(duì)軟件產(chǎn)品質(zhì)量的要求越來(lái)越高,完善和系統(tǒng)的測(cè)試是產(chǎn)品質(zhì)量強(qiáng)大的保障。本文通過(guò)為什么要做單元測(cè)試、JUnit簡(jiǎn)介、單元測(cè)試規(guī)范、JUnit4常用注解、JUnit4異常和超時(shí)測(cè)試、JUnit4常用斷言方法、JUnit4參數(shù)化測(cè)試、JUnit4測(cè)試套件等八方面的內(nèi)容概要介紹了使用JUnit進(jìn)行單元測(cè)試的相關(guān)方法,接下來(lái)隨著JUnit5的到來(lái),一個(gè)即將重新定義JVM測(cè)試方法的版本,我也繼續(xù)完善JUnit進(jìn)階內(nèi)容!

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd