您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
用JUnit測試Java類
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/2/26 11:54:58 ] 推薦標(biāo)簽:Java類 JUnit測試 單元測試

四、編寫JUnit測試(Writing a Test with JUnit)

編寫JUnit測試,只要擴展junit.framework.TestCase類可以了。你的TestCase子類將按你希望的順序調(diào)用test cases,包括可能的測試前設(shè)置和測試后清除。設(shè)置在setUp方法中進(jìn)行。清除在tearDown方法中進(jìn)行。你可以,但不是必須,重載這兩個方法做你想做的事。

下面是對上面的例子用JUnit進(jìn)行重寫的test case:

程序列表3 (CalculatorTest.java, taken from CalculatorTest.java.v2):

import junit.framework.TestCase;

public class CalculatorTest extends TestCase {

  private FactorCalculator calc;

  public CalculatorTest(String name) {
    super(name);
  }

  protected void setUp() {
    calc = new FactorCalculator();
  }

  public void testFactor() {
    int numToFactor;
    int[] factorArray;
    int[] correctFactorArray;

    numToFactor = 100;
    factorArray = calc.factor(numToFactor);
    correctFactorArray = new int[] {2, 2, 5, 5};
    assertTrue("bad factorization of " + numToFactor, isSameFactorArray(factorArray, correctFactorArray));

    numToFactor = 4;
    factorArray = calc.factor(numToFactor);
    correctFactorArray = new int[] {2, 2};
    assertTrue("bad factorization of " + numToFactor, isSameFactorArray(factorArray, correctFactorArray));

    numToFactor = 3;
    factorArray = calc.factor(numToFactor);
    correctFactorArray = new int[] {3};
    assertTrue("bad factorization of " + numToFactor, isSameFactorArray(factorArray, correctFactorArray));

    numToFactor = 2;
    factorArray = calc.factor(numToFactor);
    correctFactorArray = new int[] {2};
    assertTrue("bad factorization of " + numToFactor, isSameFactorArray(factorArray, correctFactorArray));
  }

  // presumes both factor arrays are in numeric order
  private boolean isSameFactorArray(int[] factorArray1, int[] factorArray2) {
    boolean isSame = false;
    if (factorArray1.length == factorArray2.length) {
      isSame = true;
      for(int i = 0; i < factorArray1.length; i++) {
        if (factorArray1[i] != factorArray2[i]) {
          isSame = false;
          break;
        }
      }
    }
    return isSame;
  }

  public void testIsPrime() {
    int numToCheck;
    boolean isPrime;

    numToCheck = 2;
    isPrime = calc.isPrime(numToCheck);
    assertTrue("bad isPrime value for " + numToCheck, isPrime);

    numToCheck = 3;
    isPrime = calc.isPrime(numToCheck);
    assertTrue("bad isPrime value for " + numToCheck, isPrime);

    numToCheck = 4;
    isPrime = calc.isPrime(numToCheck);
    assertFalse("bad isPrime value for " + numToCheck, isPrime);

    try {
      numToCheck = 1;
      isPrime = calc.isPrime(numToCheck);
      fail("isPrime should throw exception for numbers less than 2");
    } catch (IllegalArgumentException e) {
      // do nothing because throwing IAE is the proper action
    }
  }

  public void testIsDivisor() {
    int numToCheck;
    int potentialDivisor;
    boolean isDivisor;

    numToCheck = 6;
    potentialDivisor = 3;
    isDivisor = calc.isDivisor(numToCheck, potentialDivisor);
    assertTrue("bad isDivisor value for (" + numToCheck + ", " + potentialDivisor + ")", isDivisor);

    numToCheck = 5;
    potentialDivisor = 2;
    isDivisor = calc.isDivisor(numToCheck, potentialDivisor);
    assertFalse("bad isDivisor value for (" + numToCheck + ", " + potentialDivisor + ")", isDivisor);

    try {
      numToCheck = 6;
      potentialDivisor = 0;
      isDivisor = calc.isDivisor(numToCheck, potentialDivisor);
      fail("isDivisor should throw an exception when potentialDivisor is 0 but did not");
    } catch (ArithmeticException e) {
      // do nothing because throwing AE is the proper action
    }
  }
}

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